Forum Moderators: coopster

Message Too Old, No Replies

Stripping out multiple line breaks and turning it into just one

         

Jeremy_H

11:57 pm on Jul 11, 2006 (gmt 0)

10+ Year Member



preg_replace("/\r\n/","</p><p>",$message) seems to strip out all the line breaks and turn them into a paragraph when outputted to:

<p>$message</p>

ie:

<p>This is one paragraph\r\nThis is another paragraph</p>
turns into:
<p>This is one paragraph</p><p>This is another paragraph</p>

However preg_replace("/\r\n/","</p><p>",preg_replace("/\r\n\r\n/","\r\n",$message)) dosn't seem to solve the problem if multiple line breaks take place in a row.

<p>This is one paragraph\r\n\r\n\r\nThis is another paragraph</p>
turns into:
<p>This is one paragraph</p><p></p><p></p><p>This is another paragraph</p>
not like I had hoped:
<p>This is one paragraph</p><p>This is another paragraph</p>

Is there something I'm doing wrong?

coopster

1:00 pm on Jul 12, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You need to look for one or more occurrences of the pattern instead. This is often done using the captured pattern. There was a recent discussion asking how to Check text string for duplicates [webmasterworld.com] which will describe duplicate words. Inside that thread is another link which describes Checking string for consecutive alpha numerics [webmasterworld.com]. The concept is shown, I'll leave the exercise to you. See if you can get it, if not, let us know and we'll be happy to refine the expression with you.

Jeremy_H

5:03 pm on Jul 12, 2006 (gmt 0)

10+ Year Member



Thanks for the links,

I'm thinking that preg_replace("/(\r\n)+/","</p><p>",$message) would be the direction I should head.

Then it says to globally match one more more instance of \r\n, right?

Thanks

[edited by: Jeremy_H at 5:03 pm (utc) on July 12, 2006]

coopster

4:43 pm on Jul 13, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Right on. Nice work ;)