3

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

2
  • 5
    My Code - I doubt that. Parse error because of missing semicolon, different function names, $value instead of $node ...this code was never executed. Commented Aug 30, 2010 at 16:24
  • possible duplicate of How to delete element with DOMDocument? Commented Jul 18, 2014 at 15:09

1 Answer 1

14

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.