Skip to content

Instantly share code, notes, and snippets.

@mattijs
Created July 8, 2010 13:39
Show Gist options
  • Save mattijs/468011 to your computer and use it in GitHub Desktop.
Save mattijs/468011 to your computer and use it in GitHub Desktop.
function _getElementsByClassName(DOMNode $node, $class)
{
$elements = array();
if (XML_ELEMENT_NODE === $node->nodeType && $node->hasAttributes()) {
for ($i = 0; $i < $node->attributes->length; $i++) {
$attribute = $node->attributes->item($i);
if ('class' === strtolower($attribute->name)) {
$values = explode(' ', $attribute->value);
if (in_array($class, $values)) {
$elements[] = $node;
}
}
}
}
if ($node->hasChildNodes()) {
foreach ($node->childNodes as $childNode) {
$elements += getElementsByClass($childNode, $class);
}
}
return $elements;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment