简体   繁体   中英

PHP DOMDocument Anchor Tags

I am using DOMDocument to parse all anchor tags from a string of HTML. I need to store all the anchors which do not contain a certain href into an array. Right now I am able to loop through all the anchors and filter out the correct ones but I cannot store the original anchor. I can access the href and text values by doing things like $node->getAttribute('href') but how do I get the anchor in its original form like <a href="http://www.someplace.com">Some Text</a> Thanks! Here is the code I have now:

        $dom = new DOMDocument();
        $dom->loadHtml(mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8"));
        $anchors = array();
        foreach ($dom->getElementsByTagName('a') as $node) {
            if(strpos($node->getAttribute('href'), 'some value') !== true){
                $anchors[] = $node; // TODO: need to store the entire original anchor tag
            }
        }

Try this:

if(strpos($node->getAttribute('href'), 'some value') !== true){
    $temp = new DOMDocument();
    $temp->appendChild($temp->importNode($node, true));
    $node = $temp->saveHTML();
    //var_dump($node);
    $anchors[] = $node; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM