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

Viewed 848

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

Im getting string from DB and for now I'm trying to do something like:

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

But it keep displaying like that:

enter image description here

What I need is to get perfect:

enter image description here

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

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

enter image description here

4 Answers

Use simple CSS

p.a {
  white-space: nowrap;
}
<p class="a">
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
</p>

Related