Page 1 of 1

Determining classes inside a document, CSS classes DOM

Posted: Tue Nov 02, 2010 8:22 pm
by lovelf
Hello,

How to determine CSS classes utilized on a document,

Code: Select all

<font class="specific_class">Font</font>
a php code that would echo specific_class

I imagine opening the file, reading line by line, storing all classes found in an array and then printing the array. Do not know how to do that but know it could be done with PHP.

Thanks

Re: Determining classes inside a document, CSS classes DOM

Posted: Tue Nov 02, 2010 11:52 pm
by Weirdan

Code: Select all

    $doc = new DomDocument;
    $doc->loadHTMLFile('/path/to/file.html');
    $xml = simplexml_import_dom($doc);
    $classes = array();
    foreach ($xml->xpath('//*[@class]/@class') as $element) {
        $classes[] = (string) $element['class'];
    }
    var_dump($classes);

Re: Determining classes inside a document, CSS classes DOM

Posted: Wed Nov 03, 2010 2:47 am
by lovelf
Thanks, worked great.