PHP删除style =“”标签之间的所有内容?

时间:2011-06-30 17:15:43

标签: php preg-replace preg-match

我很有兴趣删除其中的所有内容,包括输出中的内联样式标记。例如:

style =“height:10px;”

我遇到的问题是,我发现一些php替换表达式有效,但是他们也删除了我的段落标签等。

对此有任何帮助表示赞赏。谢谢

3 个答案:

答案 0 :(得分:2)

尝试:

$html = preg_replace('%style="[^"]+"%i', '', $html);

答案 1 :(得分:2)

使用DOM Document删除标记的属性。

http://php.net/manual/en/class.domdocument.php

答案 2 :(得分:1)

这应该使用DOM和一个简单的XPath查询来查找相关元素:

<?
$doc = new DOMDocument();
$doc->loadHTML($html);
$search = new DOMXPath($doc);
$results = $search->evaluate('//*[@style]');
foreach ($results as &$result)
    $result->removeAttribute('style');
$newhtml = $doc->saveHTML();
?>
相关问题