usertest
usertest

Reputation: 27628

Remove line breaks and add BR tags in PHP

I have the following text for which I would like to add a <br> tag between every paragraph. And also remove all the line breaks. How would I do this in PHP? Thanks.

So this -

This is some text
for which I would
like to remove 
the line breaks.

And I would also 
like to place
a b>  tag after 
every paragraph.

Here is one more
paragraph.

Would become this -

This is some text for which I would like to remove the line breaks.<br/> And I would also like to place a br tag after every paragraph. <br> Here is one more paragraph.

NOTE: Ignore the highlighting of any letters.

Upvotes: 2

Views: 10620

Answers (6)

trejder
trejder

Reputation: 17495

One can consider the simplest solution possible -- built-in function nl2br:

echo nl2br($string);

Output: (Demo)

This is some text<br />
for which I would<br />
like to remove <br />
the line breaks.<br />
<br />
And I would also <br />
like to place<br />
a b>  tag after <br />
every paragraph.<br />
<br />
Here is one more<br />
paragraph.

Two things to remember:

  1. If you're using pure string (not a variable) as nl2br's argument, you must use double quotes or else your control characters, like \n or \r won't be expanded.

  2. As in the example above nl2br replaces \n with <br />\n meaning that the text is still "chopped" into multiple lines.

Upvotes: 4

mickmackusa
mickmackusa

Reputation: 47894

For a robust solution that will accommodate newline sequences from any operating system, use \R which counts \r\n or \n as a single newline sequence.

To remove any lingering horizontal whitespace characters around newline sequences, use \h* on both sides of the newline sequence.

Code: (Demo)

var_export(
    preg_replace(
        ['/\h*\R{2}\h*/', '/\h*\R\h*/'],
        ['<br>', ' '],
        $text
    )
);

Output:

'This is some text for which I would like to remove the line breaks.<br>And I would also like to place a b>  tag after every paragraph.<br>Here is one more paragraph.'

Upvotes: 0

Neal Amin
Neal Amin

Reputation: 35

This does it for me

$string = ereg_replace( "\n", "<br/>", $string);

Upvotes: 0

Gabriel Poama-Neagra
Gabriel Poama-Neagra

Reputation: 481

Well, it seems that you consider a paragraph delimiter, an empty line. So the easiest solution seems this:

$text  = str_replace( "\r", "", $text ); // this removes the unwanted \r
$lines = explode( "\n", $text ); // split text into lines.
$textResult = "";
foreach( $lines AS $line )
{
   if( trim( $line ) == "" ) $textResult .= "<br />";
   $textResult .= " " . $line;
}

I think this solves your problem. $textResult would have your result

Upvotes: 7

Thiago Silveira
Thiago Silveira

Reputation: 5133

That should work, too: (though simplistic)

$string = str_replace("\n\n", "<br />", $string);
$string = str_replace("\n", "", $string);

It was tested.

Upvotes: 5

salathe
salathe

Reputation: 51950

echo str_replace(array("\n\n", "\n"), array("<br/>", " "), $subject);

The above replaces double-newlines with the <br/> tag and any left-over single newlines into a space (to avoid words originally only separated by a newline from running into one another).

Would you have any need to cater for CRLF (windows) style line breaks; that would slightly (though not drastically) change the approach.

Upvotes: 0

Related Questions