Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Introduction</h2> <p>The best solution have seen so far is using <code>HTML Tidy</code> <a href="http://tidy.sourceforge.net/">http://tidy.sourceforge.net/</a></p> <blockquote> <p>Beyond converting the format of a document, Tidy is also able to convert deprecated HTML tags into their cascading style sheet (CSS) counterparts automatically through the use of the clean option. The generated output contains an inline style declaration.</p> </blockquote> <p>It also ensures that the HTML document is <code>xhtml</code> compatible </p> <h2>Example</h2> <pre><code>$code ='&lt;p&gt; &lt;strong&gt; &lt;span style="font-size: 14px"&gt; &lt;span style="color: #006400"&gt; &lt;span style="font-size: 14px"&gt; &lt;span style="font-size: 16px"&gt; &lt;span style="color: #006400"&gt; &lt;span style="font-size: 14px"&gt; &lt;span style="font-size: 16px"&gt; &lt;span style="color: #006400"&gt;This is a &lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;span style="color: #006400"&gt; &lt;span style="font-size: 16px"&gt; &lt;span style="color: #b22222"&gt;Test&lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;/span&gt; &lt;/strong&gt; &lt;/p&gt;'; </code></pre> <p>If you RUN </p> <pre><code>$clean = cleaning($code); print($clean['body']); </code></pre> <p>Output</p> <pre><code>&lt;p&gt; &lt;strong&gt; &lt;span class="c3"&gt; &lt;span class="c1"&gt;This is a&lt;/span&gt; &lt;span class="c2"&gt;Test&lt;/span&gt; &lt;/span&gt; &lt;/strong&gt; &lt;/p&gt; </code></pre> <p>You can get the CSS </p> <pre><code>$clean = cleaning($code); print($clean['style']); </code></pre> <p>Output</p> <pre><code>&lt;style type="text/css"&gt; span.c3 { font-size: 14px } span.c2 { color: #006400; font-size: 16px } span.c1 { color: #006400; font-size: 14px } &lt;/style&gt; </code></pre> <p>Our the FULL HTML </p> <pre><code>$clean = cleaning($code); print($clean['full']); </code></pre> <p>Output</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;style type="text/css"&gt; /*&lt;![CDATA[*/ span.c3 {font-size: 14px} span.c2 {color: #006400; font-size: 16px} span.c1 {color: #006400; font-size: 14px} /*]]&gt;*/ &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;p&gt; &lt;strong&gt;&lt;span class="c3"&gt;&lt;span class="c1"&gt;This is a&lt;/span&gt; &lt;span class="c2"&gt;Test&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt; &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>Function Used</h2> <pre><code>function cleaning($string, $tidyConfig = null) { $out = array (); $config = array ( 'indent' =&gt; true, 'show-body-only' =&gt; false, 'clean' =&gt; true, 'output-xhtml' =&gt; true, 'preserve-entities' =&gt; true ); if ($tidyConfig == null) { $tidyConfig = &amp;$config; } $tidy = new tidy (); $out ['full'] = $tidy-&gt;repairString ( $string, $tidyConfig, 'UTF8' ); unset ( $tidy ); unset ( $tidyConfig ); $out ['body'] = preg_replace ( "/.*&lt;body[^&gt;]*&gt;|&lt;\/body&gt;.*/si", "", $out ['full'] ); $out ['style'] = '&lt;style type="text/css"&gt;' . preg_replace ( "/.*&lt;style[^&gt;]*&gt;|&lt;\/style&gt;.*/si", "", $out ['full'] ) . '&lt;/style&gt;'; return ($out); } </code></pre> <p>================================================</p> <h2> Edit 1 : Dirty Hack (Not Recommended)</h2> <p>================================================</p> <p>Based on your last comment its like you want to retain the depreciate style .. <code>HTML Tidy</code> might not allow you to do that since its <code>depreciated</code> but you can do this </p> <pre><code>$out = cleaning ( $code ); $getStyle = new css2string (); $getStyle-&gt;parseStr ( $out ['style'] ); $body = $out ['body']; $search = array (); $replace = array (); foreach ( $getStyle-&gt;css as $key =&gt; $value ) { list ( $selector, $name ) = explode ( ".", $key ); $search [] = "&lt;$selector class=\"$name\"&gt;"; $style = array (); foreach ( $value as $type =&gt; $att ) { $style [] = "$type:$att"; } $replace [] = "&lt;$selector style=\"" . implode ( ";", $style ) . ";\"&gt;"; } </code></pre> <p>Output</p> <pre><code>&lt;p&gt; &lt;strong&gt; &lt;span style="font-size:14px;"&gt; &lt;span style="color:#006400;font-size:14px;"&gt;This is a&lt;/span&gt; &lt;span style="color:#006400;font-size:16px;"&gt;Test&lt;/span&gt; &lt;/span&gt; &lt;/strong&gt; &lt;/p&gt; </code></pre> <h2> Class Used </h2> <pre><code>//Credit : http://stackoverflow.com/a/8511837/1226894 class css2string { var $css; function parseStr($string) { preg_match_all ( '/(?ims)([a-z0-9, \s\.\:#_\-@]+)\{([^\}]*)\}/', $string, $arr ); $this-&gt;css = array (); foreach ( $arr [0] as $i =&gt; $x ) { $selector = trim ( $arr [1] [$i] ); $rules = explode ( ';', trim ( $arr [2] [$i] ) ); $this-&gt;css [$selector] = array (); foreach ( $rules as $strRule ) { if (! empty ( $strRule )) { $rule = explode ( ":", $strRule ); $this-&gt;css [$selector] [trim ( $rule [0] )] = trim ( $rule [1] ); } } } } function arrayImplode($glue, $separator, $array) { if (! is_array ( $array )) return $array; $styleString = array (); foreach ( $array as $key =&gt; $val ) { if (is_array ( $val )) $val = implode ( ',', $val ); $styleString [] = "{$key}{$glue}{$val}"; } return implode ( $separator, $styleString ); } function getSelector($selectorName) { return $this-&gt;arrayImplode ( ":", ";", $this-&gt;css [$selectorName] ); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload