spazzmarticus / e-css-tractor
Extracts CSS from HTML (Style-Tags)
Requires
- php: ^7.2
Requires (Dev)
- phpunit/phpunit: ^8.1
This package is auto-updated.
Last update: 2025-03-01 00:14:00 UTC
README
Extracts CSS from HTML <style>
-Tags
(Also the lamest wordplay I could possibly come up with...)
How
Installation
Install via composer
composer require spazzmarticus/e-css-tractor
Usage
//Your HTML-Document with one or more <style>-Tags $html = '<html><head><style>p { color: blue; }</style></head>...<style>h1 { background-color: green; }</style></html>'; $eCSStractor = new eCSStractor(); $css = $eCCStractor->extract($html); header("Content-type: text/css"); echo $css;
p { color: blue; } h1 { background-color: green; }
Example
Check out the example/
directory. The styles from input.html
are extracted in css.php
and used in usage.html
.
What not
This doesn't optimize the styles. GIGO ;)
Why
I built a tool to send newsletters with. The editor for the newsletter-content had to be able to work with multiple templates (each containing unique styles). By extracting the styles from the templates I was able to dynamically change the template for a preview (outside the editor) and the styles in the editor accordingly.
Before sending the newsletter the styles are written inline by emogrifier so there was no need to change anyting.