This I have found useful for dealling with class/styling removed from simplepie rss incoming feeds in wordpress.. in this example all tags have a padding:10px; style automatically added. Useful for wordpress content filters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
 
$string = "<br><p><img src='image1.jpg'></p><br><img src='image2.jpg'>";
 
 
echo add_img_style($string,'padding:10px;');
 
 
function add_img_style($string,$style) {
 
$xml = new DOMDocument();
 
@$xml->loadHTML($string);
 
    foreach($xml->getElementsByTagName('img') as $link)
    {
    $link->setAttribute('style', $style);
    }
 
$o= preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $xml->saveHTML()));
$o= str_ireplace("\n","",$o);
return $o;
 
}
 
?>