php - Automatically replace html-tags by "<br>" -


i'm looking way replace html-tags <br>.

the function str_replace won't work, because don't know attributes of tags.

the input string (e. g.):

$str = "line one<p class='one'>line two</p>line three<p/>line four</p>line five<br> line six<br /><p>line eight</p>"; 

it should convertet to:

$str = "line one<br>line two<br>line three<br>line four<br>line five<br>line six<br>line eight"; 

the tags want convert <p ....>, <br ... >, <div ....>

what's best way this?

i have no idea how replace patterns.

a quick , dirty regex be

$str = preg_replace('#<.*?>#', '<br />', $str);

that replace tags break tags, , not remove duplicates. if want that, can go 1 step further , replace multiple instances one. below adds qualifier p br , div tags should replaced.

$str = preg_replace('#</{0,1}[p|br|div].*?>#', '<br />', $str); $str = preg_replace('#(<br />)+#', '<br />', $str);` 

the better solution parse dom others have said, if want quick , easy it.


Comments