Last active
March 23, 2023 16:16
-
-
Save jongacnik/32cebca49d466157e4b9e76b543b6142 to your computer and use it in GitHub Desktop.
Get element by classname #php @classnic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getElementByClassname ($html, $classname) { | |
$dom = new DOMDocument(); | |
$dom->loadHTML($html); | |
$xpath = new DOMXpath($dom); | |
$nodes = $xpath->query('//div[@class="' . $classname . '"]'); | |
$tmp_dom = new DOMDocument(); | |
foreach ($nodes as $node) { | |
$tmp_dom->appendChild($tmp_dom->importNode($node, true)); | |
} | |
return trim($tmp_dom->saveHTML()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment