Nl2br multiple lines newline

I use nl2br to convert nl characters to a tag
, but for ex I want to convert "Hello \ n \ n \ n \ n Everybody" to "Hello
Everybody", also I want to convert multinewlines to one br tag. How can i do this?

+3
source share
3 answers

The most direct approach may be to first replace multiple lines of a newline with one using a simple regular expression:

nl2br(preg_replace("/\n+/", "\n", $input));
+8
source

If you have php 5.2.4+ you can use preg_replace and the character type of the vertical space \v

$str = preg_replace('/\v+/','<br>', $str);
+3
source

preg_replace(), nl2br HTML. nl2br(preg_replace('/\n+/', '\n', $the_string)) ().

0

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


All Articles