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 27 28 29 30 31 32 33 34
| $source = <<<HTML
<img src="xxx" style="width: 200px; height: 150px; float:left" width="100" alt="test" />
<img src="xxx" style="width: 500px; height: 300px; float:right" width="100" alt="test" />
HTML;
$doc = new DOMDocument();
$frag = $doc->createDocumentFragment();
$frag->appendXML($source);
$doc->appendChild($frag);
$images = $doc->getElementsByTagName('img');
foreach($images as $image)
{
preg_match_all('/(?P<name>[^:\s]+)\:\s*(?P<value>[^;\s]+);?/', $image->getAttribute('style'), $matches);
$styles = array_combine($matches['name'], $matches['value']);
foreach($styles as $name => $value)
{
if($name === 'float')
{
$name = 'align';
}
$image->setAttribute($name, $value);
}
$image->removeAttribute('style');
}
echo $doc->saveHTML(); |