PHP, strip_tags stripping \ n in the text area. How to stop it?

I would like to accept \n or \r\n and convert them to <br /> for use on the page. Although when a user submits a text area with new paragraphs, the strip_tags function strip_tags them of their rights. Anything I can do to keep them in line?

Thanks!!!

+4
source share
1 answer

You can use nl2br to add the line break element BR in the sequence of line break characters:

 $html = nl2br($plain); 

Note that the BR elements are just added:

 nl2br("foo\nbar") === "foo\n<br />bar" 

To prevent strip_tags from removing P and BR tags, specify them in the second parameter:

 $clean = strip_tags($html, '<p><br>'); 
+10
source

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


All Articles