This question revolves around extracting sub-elements from a DOM node based on their class names.
One method involves utilizing PHP DOM's capability to traverse the DOM via CSS selectors. To select elements by class name, employ the following syntax:
$nodes = $document->getElementsByClassName('class-name');
Alternatively, you can leverage Xpath selectors:
//*[contains(@class, 'class-name')]
For more complex queries, consider utilizing Zend_Dom_Query, which supports CSS selector syntax:
$finder = new Zend_Dom_Query($html); $nodes = $finder->query('*[class~="class-name"]');
By modifying the CSS selector, we can obtain a more efficient Xpath equivalent:
[contains(concat(' ', normalize-space(@class), ' '), ' class-name ')]
The above is the detailed content of How Can I Efficiently Select DOM Elements by Class Name in PHP?. For more information, please follow other related articles on the PHP Chinese website!