我正在寻找用 <br>
替换 html 标签的方法.
函数str_replace
不会工作,因为我不知道标签的属性。
输入字符串可能如下所示(例如):
$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>";
它应该转换为:
$str = "line one<br>line two<br>line three<br>line four<br>line five<br>line six<br>line eight";
我要转换的标签是<p ....>
, <br ... >
, 还有 <div ....>
执行此操作的最佳方法是什么?
我不知道如何替换模式。
最佳答案
$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>";
$str= preg_replace("/<p[^>]*?>/", "<br />", $str);
$str= str_replace("</p>", "<br />", $str);
echo $str= str_replace("<br /><br />", "<br />", $str);
关于php - 自动用 "<br>"替换 html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38703202/