开发者

PHP remove eveything between style="" tags?

开发者 https://www.devze.com 2023-03-16 18:25 出处:网络
I am interesting in removing everything in between and including the inline style 开发者_如何学Ctags from my output. for example:

I am interesting in removing everything in between and including the inline style 开发者_如何学Ctags from my output. for example:

style="height:10px;"

The issue I have been having is, I found some php replacement expressions that work, however they are also removing my paragraph tags and such.

Any help with this is appreciated. Thank you


Try:

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


Use DOM Document to remove the attribute of the tags.

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


This should do it using DOM and a simple XPath query to find relevant elements:

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

精彩评论

暂无评论...
验证码 换一张
取 消