开发者

PHP: Can't remove node from DOMDocument

I Can't remove node from DOMDocument(get Exception):

My Code:

<?php
    function filterElements($htmlString) {
        $doc = new DOMDocument();
        $doc->loadHTML($htmlString);
        $nodes = $doc->getElementsByTagName('a');
        for ($i = 0; $i < $nodes->length; $i++) {
          $node=$nodes->item($i)
          if (开发者_如何学Go$value->nodeValue == 'my_link') {
           $doc->removeChild($node);
          }
        }
    }
    $htmlString = '<div>begin..</div>this tool<a name="my_link">Beo</a> great!<div>.end</div>';
    filterKeyLinksElements($htmlString);
    ?>

Thanks, Yosef


First off, what exception are you getting (It likely matters).

As for the specific problem, my guess would be as follows::

The $node is not a child of the document. It's a child of its parent. So you'd need to do:

$node->parentNode->removeChild($node);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜