Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > note 70449 added to ref.dom
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 21st December 18:34
cormac
External User
 
Posts: 1
Default note 70449 added to ref.dom



Most email clients ignore stylesheets in HTML formatted emails. The best way to ensure your HTML is formatted correctly by a broad spectrum of email clients, including webmail implementations as Gmail, is to use inline style attributes. The following function uses DOM to parse an inline stylesheet, and will replace element class and id attributes with inline style attributes, and add inline style attributes for generic tag stylesheet rules. It will remove the stylesheet and any used class and id attributes as these are defunct for most email clients. It is a fairly lightweight function and does not support CSS inheritance, but will work for simple stylesheets e.g.:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>HTML EMAIL</title>
<style type="text/css">
body {
margin: 10px 10px;
font: 8pt arial;
background: #fff;
color: #000;
}
p {
margin: 0 0 10px;
line-height: 1.2em;
text-align: justify;
}
p.centered {
text-align: centre;
}
p#right {
text-align: right;
}
</style>
</head>
<body>
<p>Sample text justified</p>
<p class="centered">Centered text here</p>
<p id="right">Right-aligned text</p>
</body>
</html>

Here's the function:

<?php
function parseStyleSheetfor Email($html)
{
$doc = new DOMDo***ent;
$doc->loadHTML($html);
// grab inline stylesheet as DOM object
$oStyle = $doc->getElementsByTagName('style')->item(0);
// grab rule identifiers and rules
preg_match_all('/^([-#._a-z0-9]+) ?\{(.*?)\}/ims', $oStyle->nodeValue, $aMatches, PREG_SET_ORDER);
foreach ($aMatches as $aRule) {
$rule_id = $aRule[1];
// clean up rules
$rule = str_replace(array("\r", "\n", ' ', '; '), array('', '', ' ', ';'), $aRule[2]);
$rule = preg_replace(array('/^ /', '/;$/'), '', $rule);
// generic rules
if (!strstr($rule_id, '.') && !strstr($rule_id, '#')) {
$items = $doc->getElementsByTagName($rule_id);
// set style attribute equal to rule from stylesheet
foreach ($items as $item) {
// if there is already inline style append it to end of stylesheet rule
$current_style = $item->getAttribute('style');
if (!empty($current_style)) {
$item->setAttribute('style', $rule . ';' . $current_style);
} else {
$item->setAttribute('style', $rule);
}
}
// classes
} elseif (strstr($rule_id, '.')) {
list($rule_tag, $rule_class) = explode('.', $rule_id);
$items = $doc->getElementsByTagName($rule_tag);
foreach ($items as $item) {
$class = $item->getAttribute('class');
if ($class == $rule_class) {
// if there is already inline style append it to end of stylesheet rule
$current_style = $item->getAttribute('style');
if (!empty($current_style)) {
$item->setAttribute('style', $current_style . ';' . $rule);
} else {
$item->setAttribute('style', $rule);
}
// remove class as it won't be used now
$item->removeAttribute('class');
}
}
// ids
} elseif (strstr($rule_id, '#')) {
list($rule_tag, $id) = explode('#', $rule_id);
$item = $doc->getElementById($id);
$current_style = $item->getAttribute('style');
if (!empty($current_style)) {
$item->setAttribute('style', $current_style . ';' . $rule);
} else {
$item->setAttribute('style', $rule);
}
// remove class as it won't be used now
$item->removeAttribute('id');
}
}
// remove inline stylesheet
$oStyle->parentNode->removeChild($oStyle);
return $doc->saveHTML();
}
?>
----
Server IP: 212.108.64.74
Probable Submitter: 81.138.13.229
----
Manual Page -- http://www.php.net/manual/en/ref.dom.php
Edit -- https://master.php.net/note/edit/70449
Del: integrated -- https://master.php.net/note/delete/70449/integrated
Del: useless -- https://master.php.net/note/delete/70449/useless
Del: bad code -- https://master.php.net/note/delete/70449/bad+code
Del: spam -- https://master.php.net/note/delete/70449/spam
Del: non-english -- https://master.php.net/note/delete/70449/non-english
Del: in docs -- https://master.php.net/note/delete/70449/in+docs
Del: other reasons-- https://master.php.net/note/delete/70449
Reject -- https://master.php.net/note/reject/70449
Search -- https://master.php.net/manage/user-notes.php
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666