Search code examples
phpdomxpathsimplexmldomdocument

Identifying all class values within a html sample


I have tried searching for the past half hour for a method by which I can identify all class attribute values from a given HTML sample. I have been able to find how to find an element via xpath by class but not how to get all class element values from a given input.

So far my php method is as follows:

private function identifyDomClasses( $html )
{
    $classList = array();
    $doc       = new \DOMDocument();

    $doc->loadHTML('<html><body>'. $html .'</body></html>');

    $xml       = simplexml_import_dom($doc);

    // Code to identify all class attributes and push them onto the $classList array

    return $classList;
}

I'd rather not result to using regex, but if this isn't possible with xpath I will go down that route instead.


Solution

  • What about:

    //@class
    

    It will give you all class attributes. You can then loop over them and do something with them, like print them out. Since you are returned a set of attribute nodes, you can still access the parent element containing the attribute.