Is there anything like GetElementByClass
?
Here is an implementation I whipped up...
function getElementsByClassName(DOMDocument $domNode, $className) {
$elements = $domNode->getElementsByTagName('*');
$matches = array();
foreach($elements as $element) {
if ( ! $element->hasAttribute('class')) {
continue;
}
$classes = preg_split('/\s+/', $element->getAttribute('class'));
if ( ! in_array($className, $classes)) {
continue;
}
$matches[] = $element;
}
return $matches;
}
This version doesn't rely on the helper function above.
$str = '<body>
<a href="">a</a>
<a href="http://example.com" class="tracker">a</a>
<a href="http://example.com?hello" class="tracker">a</a>
<a href="">a</a>
</body>
';
$dom = new DOMDocument;
$dom->loadHTML($str);
$anchors = $dom->getElementsByTagName('body')->item(0)->getElementsByTagName('a');
foreach($anchors as $anchor) {
if ( ! $anchor->hasAttribute('class')) {
continue;
}
$classes = preg_split('/\s+/', $anchor->getAttribute('class'));
if ( ! in_array('tracker', $classes)) {
continue;
}
$href = $anchor->getAttribute('href');
$url = parse_url($href);
$attach = 'stackoverflow=true';
if (isset($url['query'])) {
$href .= '&' . $attach;
} else {
$href .= '?' . $attach;
}
$anchor->setAttribute('href', $href);
}
echo $dom->saveHTML();
Output
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<a href="">a</a>
<a href="http://example.com?stackoverflow=true" class="tracker">a</a>
<a href="http://example.com?hello&stackoverflow=true" class="tracker">a</a>
<a href="">a</a>
</body></html>