Tuesday, February 16, 2010

Improved version of inline style swap for mail

// version 2 capturing all styles not just dreamweaver style
// first we will capture the style block and assign it to a new var - block
$wad = preg_match("/<style[\s\S]*?style[ \t\n\r]*>/i", $file, $fgg);
$block = $fgg[0];

// wer captures the whole style name and definition into the $hello array
$wer = preg_match_all("/\.[^\.]+?}/is", $block, $hello);

// rest is now the whole style class declaration
// for each value in the array we will loop through the code and swap the styles in
foreach(array_unique($hello[0]) as $rest) {
$rest = trim($rest);
$rest = preg_replace('/\s+/', ' ', $rest);
// now we split the style names and definitions into 2 different arrays
// $h2 is the style name and $h3 is the associated definition
preg_match_all("/(?<=\.)\S+(?=\s*?{)/i", $rest, $h2);
preg_match_all("/(?<=\{)[^}]*/i", $rest, $h3);
$styledef = trim ($h3[0][0]);
$simplestylename = trim ($h2[0][0]);
$clssub = 'class="'.$simplestylename.'"';
$clsrp = 'style="'.$styledef .'"';
$file = str_replace($clssub,$clsrp,$file);