Forum Moderators: coopster

Message Too Old, No Replies

How to replace "\n\n" with "</p><p>"

Detecting two line breaks into a string

         

cuesta

10:37 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



Hi.

I've got this text into a field from my DB:
---------//-----------
Kerry issued a statement after Berger stepped down.

Sandy Berger is my friend, and he has tirelessly served his nation with honor and distinction.
---------//------------

So between 'down' and 'Sandy' there are two line breaks, right?

But if I perform this:
$mystr = str_replace("\n\n", "</p><p>", $mystr);
echo $mystr;

There are no "</p><p>" inserted.

Could anybody explain me how to solve this? Thank you very much.

sned

10:48 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



I had this same issue a while back with a textarea on a form. It turned out that instead of just \n for a newline, it was \r\n. Perhaps this was just because people were cutting/pasting from Word.

also not sure if you just want breaks, or the <p />'s for formatting, but you could look into the nl2br function.

hth

-sned

Longhaired Genius

10:52 pm on Jul 20, 2004 (gmt 0)

10+ Year Member



Here's a useful thread on this subject: [webmasterworld.com...]

Birdman

10:54 pm on Jul 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You may need to look for "\r\n\r\n". Different Operating systems write line breaks differently.

$mystr = str_replace("\n\n", "</p><p>", $mystr);

$mystr = str_replace("\r\n\r\n", "</p><p>", $mystr);

Birdman

yous guys are fast!

cuesta

9:32 am on Jul 21, 2004 (gmt 0)

10+ Year Member



For me
$mystr = preg_replace('/\n?(.+?)(\n\nŠ\z)/s', "</p><p>", $mystr);

works very well.

Thank you very much. And yes, you guys're very fast ;)