Saturday 1 February 2014

Grabbing the href attribute of an A element

87 down vote accepted

Reliable Regex for HTML are difficult. Here is how to do it with DOM:
$dom = new DOMDocument;
$dom
->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node) {
echo $dom
->saveHtml($node), PHP_EOL;
}
The above would find and output the "outerHTML" of all A elements in the $html string.
To get all the text values of the node, you do
echo $node->nodeValue; 
To check if the href attribute exists you can do
echo $node->hasAttribute( 'href' );
To get the href attribute you'd do
echo $node->getAttribute( 'href' );
To change the href attribute you'd do
$node->setAttribute('href', 'something else');
To remove the href attribute you'd do
$node->removeAttribute('href'); 
You can also query for the href attribute directly with XPath
$dom = new DOMDocument;
$dom
->loadHTML($html);
$xpath
= new DOMXPath($dom);
$nodes
= $xpath->query('//a/@href');
foreach($nodes as $href) {
echo $href
->nodeValue; // echo current attribute value
$href
->nodeValue = 'new value'; // set new attribute value
$href
->parentNode->removeAttribute('href'); // remove attribute
}
Also see:

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More