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

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 <br> tags generally look like either :

  • <br>
  • or <br/>, with any number of spaces before the /

I suppose something like this would do the trick :

$html="this <br>is<br/>some<br />text <br    />!";
$nl = preg_replace('#<br\s*/?>#i', "\n", $html);
echo $nl;

Couple of notes :

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

Leave a Comment