我的代码:
<?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);
?>
谢谢,
优素福
解决方法:
首先,您要获得什么例外(可能很重要).
至于具体的问题,我的猜测如下:
$node不是文档的子级.这是其父母的孩子.因此,您需要执行以下操作:
$node->parentNode->removeChild($node);