Tuesday, November 10, 2009

Replacing tag based class definitions with inline styles

Again, this can be useful for parsing HTML bound for rich text email, where you need to use inline styles -- for example when sending to a gmail account. this will scoop out the styles and plop them into the tag itself, it also will replace double quotes with single quotes for those pesky font familes that include spaces in the name

// PHP 4
// attempt to replace tag based class definitions with inline styles
// add tags to the array to expand the scope

$sar = array('a','h1','h2','h3','h4','p','ul','li');

$i = 0;
$sarsize = sizeof($sar);
$tag = $sar[$i];
$pattern = '/(?<='.$tag.'.\{)[^}]*/i';
$style = preg_match ($pattern, $file, $smem);
$attributes = $smem[0];
if ($attributes){
$attributes = str_replace('"',"'",$attributes);
$attributes = preg_replace('/\s+/i', ' ', $attributes);
$attributes = trim($attributes);
$file = str_replace('<'.$tag,'<'.$tag.' style="'.$attributes.'"',$file);
$i = 0;