CssToInlineStyles class

CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline styles. This is very usefull when you're sending emails.

Grab your copy

Download CssToInlineStyles (1.0.3)

Follow development on GitHub

Follow CssToInlineStyles

About

PHP CssToInlineStyles is a class to convert HTML into HTML with inline styles.

License

CssToInlineStyles is BSD licensed.

Documentation

The class is well documented inline. If you use a decent IDE you'll see that each method is documented with PHPDoc.

More documentation

Sites that use this class

Do you know a site that uses this class? Use the form below.

Comments

Tijs Verkoyen

Tijs Verkoyen wrote:
15/05/2010

Changelog since 1.0.1
- fixed some stuff on specifity

Changelog since 1.0.0
- rewrote the buildXPathQuery-method
- fixed some stuff on specifity
- added a way to use inline style-blocks

lmeurs

lmeurs wrote:
08/07/2010

Hey Thijs,

Great script you developed, thanks a lot for that!

One question: if one defines general styles for some headings like ie. h1,h2,h3 {...} and later on a specific style for a heading like ie. h1 {...}, the latter addes styles are ignored.

I'm fairy new to PHP and tried to solve it in the sortOnSpecifity function, but unfortunately I did not succeed.

Not a big bug, but a bug! :-)

Thanks again, cheers!

Laurens (Rotterdam)

Jeremy Shipman

Jeremy Shipman wrote:
15/07/2010

Hi Tijs, thanks for sharing your code with the world.

I'm getting a strange bug where
<p>

Jeremy Shipman

Jeremy Shipman wrote:
15/07/2010

...seems my comment didn't come out fully.

I'm getting an bug where paragraphs containing only a non breaking space character suddenly also have another character in them:

Jeremy Shipman

Jeremy Shipman wrote:
15/07/2010

Hello again Tijs, I've found the problem... it was to do with the character encoding using the utf8_decode fixed it.

Peter

Peter wrote:
20/07/2010

Hey there Tijs.

Thanks for the great script! Question though:

In my HTML I have...

<div class="something">...</div>

...but CssToInlineStyles ignores the class as written in my stylesheet...

.something {
font-size: 11px;
line-height: 13px;
}

Why is the above class ignored as written? The class DOES get applied however if written like this...

div.something {
font-size: 11px;
line-height: 13px;
}

Which is fine unless it's a class that I'd like to apply to many things such as div, p, td, span, etc.

Is there any way to have a class applied without having to precede it with a specific tag in the stylesheet?

Thank you kindly!
Peter

Peter

Peter wrote:
20/07/2010

Does the problem I mentioned above maybe result from specificity? The div I mentioned above is wrapped in another div also like so...

<div #content>
<div class="something">...</div>
</div>

In my stylesheet, the #content selector also has font-size and line-height set. Is #content overriding .something? In the code above, I would think that .something should override #content... no?

Actually, I can't seem to apply any classes. Well, unless they are preceded by an html tag in the stylesheet like so div.something or span.something.

I'm guessing the container div with #content is overriding all the classes inside of it... which is not what happens in my web pages. Just to be sure, I tried changing the selector in my stylesheet from ".something" to "#content .something" but the class still isn't being applied. Any idea how to apply classes properly?

Thanks again!
Peter

Tijs Verkoyen

Tijs Verkoyen wrote:
27/08/2010

Changelog since 1.0.2
- .class are matched from now on.
- fixed issue with #id
- new beta-feature: added a way to output valid XHTML (thx to Matt Hornsby)

Daniel Ritter

Daniel Ritter wrote:
22/10/2010

Hi

I like your scipt. It's very usefull. But I have a problem with the umlauts. Umlauts like 'öäüÖÄÜ...' are converted to 'äpülöäÃoeÄÖ' or like this.

Could you fix this problem? Umlauts should be the same and not so special letters.

Thank you!

Daniel Ritter

Daniel Ritter wrote:
22/10/2010

Here on this website it's the same. :) It should be 'UOAuoa...' with points on top. This is important for the german language.

Daniel Ritter

Daniel Ritter wrote:
23/10/2010

It's okey. It was a problem by me. I decoded it with utf8_decode(). Now it's working.

Tijs

Tijs wrote:
25/10/2010

@Daniel: glad it's fixed.

Leon Bogaert

Leon Bogaert wrote:
28/10/2010

Hi Tijs,

Works like a charm. I changed one thing:

'*[ contains( concat( " ", @class, " " ), concat( " ", "\1", " " ) ) ]' => '//*[ contains( concat( " ", @class, " " ), concat( " ", "\1", " " ) ) ]'

That worked better for me.

Groeten,
Leon

Bart

Bart wrote:
04/12/2010

Supertool, great stuff.
Works easy, in 2 minutes running

You can add this website as a reference: www.printenbind.nl

Madhuraka

Madhuraka wrote:
25/12/2010

Hi Tijs,

This is a great tool and saved hours of my work. I had to convert about 100 odd html scripts to inline format and it took few seconds!!!

Thanks for sharing it

All the best

Jim Reijers

Jim Reijers wrote:
04/02/2011

Verzoekje, Thijs:

Ik moet de inhoud van twee .css bestanden inladen (via file_get_contents). En de regels in het tweede bestand moeten die van het eerste bestand overrulen. Maar hij houdt toch de regels van het eerste bestand aan, ook al wissel ik de volgorde om.

Hoe kan dat?

pieter

pieter wrote:
18/02/2011

hey tijs,

awesome class, easy to implement and easy to use, thanks for sharing!

pieter

Lrnt

Lrnt wrote:
13/10/2011

You just made my day ! no my week ! no my year !
A-ma-zing !

Saeid

Saeid wrote:
25/11/2011

@Daniel,
Thank you for utf8_decode().

Chris

Chris wrote:
15/12/2011

Thank you for writing such a useful class! You've just saved me hours of coding!

Add your comment

If you have some feedback, a remark or you just want to say something. Go ahead, use the form below.

 

:

:

:

About me?

Well, I'm Tijs Verkoyen, a self employed Belgian PHP-developer at Sumo Coders.

If you want to know more about me, check my blog (Dutch).

If you really like my work and want to support me, feel free to use the donate-button below.

Spoon

At both companies, we use Spoon. Spoon is a PHP5 Library that we use for building all our Web 2.0 applications.

The classes that you can download from this website are standalone, so each developer is free to use a library/framework/… he likes best.