php: replacing double
with

请求翻译

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

hello first line

this is a second line
this is a 3rd line

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

hello first line

this is a second line
this is a 3rd line

so my current solution is this:

  1. i need to trim the $data for
    at the start/end of the string
  2. replace all strings that have 2
    or more with

    (one single
    is allowed).
  3. finally, add

    at the start and at the end

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

function replace_br($data) {
 # step 1
 $data = trim($data,'

'); $data = trim($data,'

'); $data = trim($data,'
'); # step 2 ??? // preg_replace() ? # step 3 $data = '

'.$data.'

'; return $data; }

thanks!

ps: it would be even better to avoid specific situations. example: "hello




too much space
" -- those 5 breaklines should also be converted to just one "

"

final solution (special thanks to kemp!)

function sanitize_content($data) {
    $data = strip_tags($data,'

,
,,,,,,

,
    ,
      ,
    • ,'); $data = trim($data,'

      '); $data = trim($data,'

      '); $data = trim($data,'
      '); $data = preg_replace('#(?:\s*?){2,}#','

',$data); $data = '

'.$data.'

'; return $data; }

3个回答