11 maart 2010

Convert CSS to inline styles with PHP

When sending HTML-mails through PHP you'll see that some email-clients (like: gmail) will strip al the styles you defined in an external stylesheet or in a style-tag.

You can't expect the users of your application will define all the styles inline (some WYSIWYG-editors have that functionality), so it would be nice if there was a way to grab the HTML, set the CSS and just get the HTML but with all styles defined inline.

So here is CSSToInlineStyles, this class enables you to do this.

How does it work?

Well, it's pretty straight forward. After setting the HTML (and CSS), the class will read the CSS build an array that contains all the rules sorted by their specifity.

The HTML will be loaded as a DOMDocument.

When that's done, each rule will be looped. The CSS-selector will be converted into a XPath-query. If the query returns an element a style-attribute will be added, all defined CSS-rules will be added into this style attribute.

So, get the class and enjoy.

Dit artikel gaat over: , , , , , , . Convert CSS to inline styles with PHP werd geschreven door Tijs Verkoyen.
Er zijn al 11 reacties.

Reacties

Daniel

Daniel schreef:
26/04/10

Hi Tijs,

Your class seemed really useful to me, but somehow it refuses to work with classes. Nodes with id's are replaced just fine.

Does this XPath query look strange to you:

"//[ contains( concat( \" \", @class, \" \" ), concat( \" \", \"crSectionHeader\", \" \" ) ) ]"

It's one of the queries that are generated. It should select nodes of class crSectionHeader but fails and returns false. Any ideas? (Should it start with '\\*...'?)

The nodes look like this: '<div class="crSectionHeader">...</div>'. So nothing fancy there.

Regards,
Daniel

Gajus Kuizinas

Gajus Kuizinas schreef:
26/07/10

There is an issue with UTF8 pages. It happens when we try to load UTF8 to DOMDocument. I tried multiple fixes, but the one that really worked was encoding the $this->html before loading it with loadHTML to HTMLENTITIES mb_convert_encoding($this->html, 'HTML-ENTITIES', "UTF-8")

Gajus Kuizinas

Gajus Kuizinas schreef:
26/07/10

And just in case, to decode the string before returning use html_entity_decode($this->html, NULL, 'UTF-8'); NOT mb_encoding as this will cause encoding problems.

Gajus Kuizinas

Gajus Kuizinas schreef:
26/07/10

megh.. sorry for this. please concatenate my comments. :) in the decode part replace $this->html to $html.

Chris

Chris schreef:
19/01/11

Could you give a code example of using the setCleanup() and setUseInlineStylesBlock() functions on the documenation page for this class? I'd be grateful. Thank you for creating this.

Tijs Verkoyen

Tijs Verkoyen schreef:
24/03/11

Andrew King

Andrew King schreef:
16/06/11

Thanks Gajus. I was getting stuck but that worked for me too.

chris

chris schreef:
01/08/11

ok thank you guys very much

Saeid

Saeid schreef:
27/11/11

@Gajus, Thank you. mb_convert_encoding worked for me.

juanhdv

juanhdv schreef:
29/11/11

This script is failing in converting <a> tags. Any clues?
Is the only tag that is not processed through the html.

FlorCoppola

FlorCoppola schreef:
07/12/11

Thanks Tijs, your class saved me lots of time,I noticed that it doesn't work if my styles rules are defined like this : #someRule .childElement.
But is ok anyway, a big Thank you!

Reageer velden gemarkeerd met een sterretje zijn verplicht

*