from a html source, using dom and xpath how would I return an array containing all the div class names? I use the following which returns all the div class names that contain “XX”, but don’t know the query to use for $targets to return all. I know this should be simple but I can’t find one that works.
JavaScript
x
$doc = new DOMDocument();
$doc->loadHTML($data);
$xpath = new DOMXpath($doc);
$targets = $xpath->query(".//div[contains(@class, 'XX')]");
$classNames_array = array();
foreach ($targets as $target) {
$classNames_array[] = $target->textContent;
}
print_r(array_values($classNames_array));
Thanks.
Advertisement
Answer
The query for all classes of divs would be '//div/@class'
. Then you get a DomAttr list which you can iterate and get the value as a public property, e.g.
JavaScript
foreach ($entries as $entry) {
$classes[] = $entry->value;
}