如果具有带有p标签的html属性而不具有属性,并且正则表达式为:
$html = preg_replace("/<p[^>]*>(.+?)<\/p>/i", "<p>$1</p>", $html);
如果p标签没有像这样的任何新行,则正则表达式运行良好
<p style="text-align: center;">It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout</p>
但是,当p标签有换行时,上面的正则表达式将无法正常工作。举个例子
<p style="text-align: center;">It is a long established fact that a reader will be
distracted by the readable <br />
content of a page when looking at its layou</p>
因此,有人可以建议在上述正则表达式中需要进行哪些更改,以便在p标签包含包含新行的字符串时它们可以正常工作?
如果必须,请使用
$html = preg_replace("/<p[^>]*>(.+?)<\/p>/is", "<p>$1</p>", $html);
# ^
启用该singleline
模式,也就是点也匹配换行符。但是,通常的警告是不要在HTML
标签上使用正则表达式。
参见regex101.com上的演示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句