views:

20

answers:

1

Hi,

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

+1  A: 

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);
ircmaxell
Thanks! its works.
Yosef