php preg_replace删除<br/>与样式attr

时间:2011-02-25 16:52:31

标签: php regex html-parsing

<br style="clear: both">

以下正则表达式对我不起作用,我做错了什么?

return preg_replace('#<br[^>]+style="clear:both"[^/>]#is', '',  $output);
谢谢。

3 个答案:

答案 0 :(得分:1)

如果你的字符串总是:

<br style="clear: both">

您可以改为使用str_replace

return str_replace('<br style="clear: both">', '',  $output);

Beware that you shouldn't use regex for html manipulation.

改为使用一些解析器HTML。

答案 1 :(得分:1)

你可以逃避 = &lt; &gt; 等字符。 像这样:

<?php    
return preg_replace('#\<br[^>]+style\=\"clear\:both\"[^/>]#is', '',  $output);
?>

更好的例子:

<?php
return preg_replace('#\<br*.?\>#is', '',  $output);
?>

答案 2 :(得分:0)

试试这个:

#<br *style="clear: *both"/?>#is
相关问题