Reliable Regex for HTML are difficult. Here is how to do it with DOM:
  
To get all the text values of the node, you do
  
  
  
  
  
  
  
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node) {
    echo $dom->saveHtml($node), PHP_EOL;
}A elements in the $html string. To get all the text values of the node, you do
echo $node->nodeValue; href attribute exists you can doecho $node->hasAttribute( 'href' );href attribute you'd doecho $node->getAttribute( 'href' );href attribute you'd do$node->setAttribute('href', 'something else');href attribute you'd do$node->removeAttribute('href'); 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
} 










0 comments:
Post a Comment