How to remove unnecessary spaces from a string, so there are no extra spaces in HTML

How to remove unnecessary spaces from a string, so there are no extra spaces in HTML?

I get a row from the DB, and now I'm trying to do something like:

nl2br(trim(preg_replace('/(\r?\n){3,}/', '$1$1', $comment->text)));

But it continues to display as follows:

enter image description here

I need it to work perfectly:

enter image description here

How it's done??? Because I'm bad at regex :(

EDIT: $ comment-> text contains text from DB:

enter image description here

+4
source share
3 answers
preg_replace('/(\r)|(\n)/', '', $comment->text);

Exit

"1 2"<br>"2 3"<br>"3"<br>"4"<br>"5"
+1
source

You can just use this str_replace if you want to remove spaces

$string = str_replace(' ', '', $string);

or if you want to delete all whitespeces use this

$string = preg_replace('/\s+/', '', $string);
0
source

, <br>, :

$string = '12
23
3
4
5
6';

var_dump(implode("\n<br>\n", preg_split('/(\r?\n)+/', $string)));

var_dump(preg_replace('/(\r?\n)+/', "\n<br>\n", $string));

:

string(38) "12
<br>
23
<br>
3
<br>
4
<br>
5
<br>
6"

string(38) "12
<br>
23
<br>
3
<br>
4
<br>
5
<br>
6"
0

All Articles