php: replacing double <br /> with </p><p>

Posted by andufo on Stack Overflow See other posts from Stack Overflow or by andufo
Published on 2010-06-02T16:43:28Z Indexed on 2010/06/02 16:53 UTC
Read the original article Hit count: 312

Filed under:
|
|

i use nicEdit to write RTF data in my CMS. The problem is that it generates strings like this:

hello first line<br><br />this is a second line<br />this is a 3rd line

since this is for a news site, i much prefer the final html to be like this:

<p>hello first line</p><p>this is a second line<br />this is a 3rd line</p>

so my current solution is this:

  1. i need to trim the $data for <br /> at the start/end of the string
  2. replace <br /><br /> with </p><p> (one single <br /> is allowed).
  3. finally, add <p> at the start and </p> at the end

i only have the 3rd step so far. can someone give me a hand with steps 1 and 2?

function replace_br($data) {
 # step 3
 $data = '<p>'.$data.'</p>';
 return $data;
}

thanks!

ps: it would be even better to avoid specific situations. example: "hello<br /><br /><br /><br /><br />too much space" -- those 5 breaklines should also be converted to just one "</p><p>"

© Stack Overflow or respective owner

Related posts about php

Related posts about html