Home > Software engineering >  nl2br function producing whitespace after newline
nl2br function producing whitespace after newline

Time:12-05

The following code produces a whitespace after each newline and I don't know why. Please help, how can I make the same functionality without adding whitespaces?

<?php

$test =  "Hello World\nHello World";
$test = preg_replace ( "/([^\s]{80}?)/" , "\\1<br />" , trim ( nl2br ( strip_tags ( $test, '<br>' ) ) ) );
echo $test;`

?>

CodePudding user response:

I did the test and I didn't notice any whitespace after the use of the nl2br() function.

$string =  "Hello World\nHello World";
$string = nl2br ($string);

Output

Hello World<br />\n
Hello World

It is worth noting that this function preserves the newlines.

  • Related