繁体   English   中英

PHP DOMDocument锚标记

[英]PHP DOMDocument Anchor Tags

我正在使用DOMDocument来解析HTML字符串中的所有锚标签。 我需要将不包含某个href的所有锚点存储到数组中。 现在,我能够遍历所有锚点并筛选出正确的锚点,但是我无法存储原始锚点。 我可以通过执行$node->getAttribute('href')类的操作来访问href和text值,但是如何获得其原始格式的锚点,如<a href="http://www.someplace.com">Some Text</a>谢谢! 这是我现在拥有的代码:

        $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
            }
        }

尝试这个:

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; 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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