1

I have a HTML editor where the admin can enter their content for a HTML email, but for consistent styles between browsers/clients this needs to include inline css. E.g for a paragraph:

Convert:

<p class="standard">

Into:

<p class="standard" style="-ms-text-size-adjust:100%; mso-line-height-rule:exactly; font-family:Helvetica, Arial, sans-serif; font-size:12px; line-height:18px; color:<?php echo $body_font_color; ?>; margin-top:0px; margin-bottom:0px;">

I think this is the same as the popular Mail Chimp works by adding inline styles if they dont exist.

Paragraphs are just example, I also have tables and other classes to add styles into.

2

2 Answers 2

3

Load the document into a PHP DOMDocument object. Then you'll have all the methods you need to traverse the DOM tree and make whatever changes you need.

For example:

$doc = DOMDocument::loadHTML($html);

foreach($doc->getElementsByTagName('p') as $para){
  // Get existing style
  if($para->hasAttribute('style')){
    $currStyle = $para->getAttribute('style');
    $para->removeAttribute('style');
  } else {
    $currStyle="";
  }

  // Perform whatever operations on the style you want.

  // comletely replace existing style.
  $para->setAttribute('style','your style string here');
}
$newdoc = $doc->saveHTML();

The PHP reference is here

Sign up to request clarification or add additional context in comments.

Comments

2

There are tools like inline styler, which aims to make all CSS inline for e-mails.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.