How about this? it supports identifiers and eliminates duplicates and can process some elements that will be displayed only once, and not li [1] li [2]
<?php header('Content-type: text/plain'); $source = '<body class="theme"> <div class="header"> <h1><a href="#">Welcome</a></h1> </div> <div class="content"> <div id="ff"> </div> <div class="sidebar"> <ul> <li id="hh"><a href="#">Link 1</a></li> <li><a href="#">Link 2</a></li> <li><a href="#">Link 3</a></li> <li><a href="#">Link 4</a></li> <li><a href="#">Link 5</a></li> </ul> </div> <div class="main"> <h2>Main Heading</h2> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\ standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p> </div> </div> <div class="footer"> <p>Copyright © 2012 Me!</p> </div> </body>'; $html = new DOMDocument; $html->loadHTML($source); $nodes = $html->getElementsByTagName("*"); $css = array(); $paths=array(); for ($i = 0; $i < $nodes->length; $i++) { $node = $nodes->item($i); $key=$node->getNodePath(); if(!$node->hasAttribute("class") && !$node->hasAttribute("id")) $paths=addPath($paths,$key,"",$node->nodeName); $path=process($paths,$key); if (!$path)continue; $css[$key]=str_replace(array("/"), array(" "), $path) . " {}\n"; if ($node->hasAttribute("class")) { $class=$node->nodeName . "." . $node->getAttribute("class"); $paths=addPath($paths,$key,$class,$node->nodeName); $path=process($paths,$key); $css[$key]=str_replace(array("/"), array(" "), $path) . " {}\n"; } elseif ($node->hasAttribute("id")) { $id="#" . $node->getAttribute("id"); $paths=addPath($paths,$key,$id,$node->nodeName); $path=process($paths,$key); $css[$key]=str_replace(array("/"), array(" "), $path) . " {}\n"; } } $css=implode("",array_unique($css)); echo $css; function addPath($paths,$path,$class,$name){ $once_elems=array( 'li','a' ); $sourcePath=explode("/",$path); if ($sourcePath[1]=="html"){ unset($sourcePath[1]); } $path=implode("/",$sourcePath); if (!$class && isset($paths[$path])){ return $paths; } if ((in_array($name,$once_elems) && !$class) || !$class){ $class=$name; } $paths[$path]=$class; return $paths; } function process($paths,$path){ $sourcePath=explode("/",$path); if ($sourcePath[1]=="html"){ unset($sourcePath[1]); } $sourcePath=array_values($sourcePath); $path=implode("/",$sourcePath); foreach ($paths as $k=>$v){ if (!$k)continue; $ex=explode("/",$k); if (strpos($path,$k,0)!==false){ $sourcePath[count($ex)-1]=$v; } } $path=implode("/",$sourcePath); return $path; } ?>
Conclusion:
body.theme {} body.theme div.header {} body.theme div.header h1 {} body.theme div.header h1 a {} body.theme div.content {} body.theme div.content #ff {} body.theme div.content div.sidebar {} body.theme div.content div.sidebar ul {} body.theme div.content div.sidebar ul #hh {} body.theme div.content div.sidebar ul #hh a {} body.theme div.content div.sidebar ul li {} body.theme div.content div.sidebar ul li a {} body.theme div.content div.main {} body.theme div.content div.main h2 {} body.theme div.content div.main p {} body.theme div.footer {} body.theme div.footer p {}
source share