PHP:无法从DomDocument中删除节点 [英] PHP: Can't remove node from DomDocument

查看:57
本文介绍了PHP:无法从DomDocument中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从DomDocument中删除节点(出现异常):

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

我的代码:

<?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);
    ?>

谢谢,
Yosef

Thanks, Yosef

推荐答案

首先,您会得到什么例外(可能很重要)。

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

对于特定的问题,我的猜测是如下:

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

$ node 不是文档的子级。这是其父母的孩子。因此,您需要执行以下操作:

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

这篇关于PHP:无法从DomDocument中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆