How to replace all XHTML/HTML line breaks (
) with new lines?

前端 未结 4 1258
死守一世寂寞
死守一世寂寞 2020-11-30 04:50

I am looking for the best br2nl function. I would like to replace all instances of
and

4条回答
  •  温柔的废话
    2020-11-30 05:19

    I would generally say "don't use regex to work with HTML", but, on this one, I would probably go with a regex, considering that
    tags generally look like either :


    • or
      , with any number of spaces before the /


    I suppose something like this would do the trick :

    $html = 'this 
    is
    some
    text
    !'; $nl = preg_replace('##i', "\n", $html); echo $nl;

    Couple of notes :

    • starts with
    • followed by any number of white characters : \s*
    • optionnaly, a / : /?
    • and, finally, a >
    • and this using a case-insensitive match (#i), as
      would be valid in HTML

提交回复
热议问题