Determining classes inside a document, CSS classes DOM

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Determining classes inside a document, CSS classes DOM

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Determining classes inside a document, CSS classes DOM

Post 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);
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Determining classes inside a document, CSS classes DOM

Post by lovelf »

Thanks, worked great.
Post Reply