levi
levi

Reputation: 25091

PHP remove eveything between style="" tags?

I am interesting in removing everything in between and including the inline style tags 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

Upvotes: 1

Views: 2192

Answers (3)

Edoardo Pirovano
Edoardo Pirovano

Reputation: 8334

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();
?>

Upvotes: 2

Pheonix
Pheonix

Reputation: 6052

Use DOM Document to remove the attribute of the tags.

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

Upvotes: 2

Emre Yazici
Emre Yazici

Reputation: 10174

Try:

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

Upvotes: 2

Related Questions