Detect paragraph in form

How can I find that there are different paragraphs in the form? In this example, if the user writes different paragraphs, the echo puts everything in a number. I tried a space: pre, and that didn't work. I don’t know what else can be done to echo the text with <p> ?

CSS

 #text { white-space:pre; } 

HTML:

 <form action='normal-html.php' method='post'> <textarea id="text" name='text' rows='15' cols='60'></textarea> <br/> <input type='submit' value='Convertir a html' /> </form> <br /> <?php $text = $_POST[text]; echo $text; ?> 
+4
source share
2 answers

It sounds like work for http://php.net/manual/en/function.nl2br.php

 string nl2br ( string $string [, bool $is_xhtml = true ] ) Returns string with '<br />' or '<br>' inserted before all newlines (\r\n, \n\r, \n and \r). 

You can use this when you echo data, so you never change what is in the database, or you can simply change the user input when it is saved in the database. Personally, I'm a fan of the first option, but all that works best for your application.

Edit: if you want to use the <p> tags, you can also do this using str_replace :

 $text = '<p>'; $text.= str_replace('\n', '</p><p>', $_POST[text]); 

\n usually a new line, depending on how it is read, you may need to use \r\n , and replacing the line will do the rest. This will leave a spare <p> at the end of the line, but you will see where this happens.

+5
source

You can use the explode function ( php man page ):

 $your_array = explode("\n", $your_string_from_db); 

Example:

 $str = "Lorem Ipsum\nAlle jacta est2\nblblbalbalbal"; $arr = explode("\n", $str); foreach ( $arr as $item){ echo "<p>".$item."</p>"; } 

Output:

  Lorem Ipsum Alle jacta est blblbalbalbal 
+1
source

Source: https://habr.com/ru/post/1499802/


All Articles