Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Last active March 23, 2023 16:16
Show Gist options
  • Save jongacnik/32cebca49d466157e4b9e76b543b6142 to your computer and use it in GitHub Desktop.
Save jongacnik/32cebca49d466157e4b9e76b543b6142 to your computer and use it in GitHub Desktop.
Get element by classname #php @classnic
<?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