PHP:无法从DomDocument中删除节点

Yos*_*sef 2 php domdocument

我无法从DomDocument中删除节点(获取异常):

我的代码:

<?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 ($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);
    ?>
Run Code Online (Sandbox Code Playgroud)

谢谢你,约瑟夫

irc*_*ell 12

首先,你得到了什么例外(这很重要).

至于具体问题,我的猜测如下::

$node不是文件的孩子.它是其父母的孩子.所以你需要这样做:

$node->parentNode->removeChild($node);
Run Code Online (Sandbox Code Playgroud)