DOMDocument help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cdw3423
Forum Newbie
Posts: 3
Joined: Tue Oct 20, 2009 10:50 pm

DOMDocument help

Post by cdw3423 »

I need to modify some html documents and was hoping I can figure out how to do it using DOMDocument. I need to do three things.
1) change the class of any div that currently has a class of 'footnotehilite' to 'footnote'. that proved to be very easy...

Code: Select all

 
      if($NodeName == 'div' AND $class == 'footnotehilite'){
        $element->removeAttribute('class');
        $element->setAttribute  ('class', 'footnote');
      }
 
2) find nodes that are of type span with a class of 'footscript' then remove the span but leave everything inside the span. I can find the span in question and change the class like in the previous example. But I have no idea how to get all the elements and text inside the span and replace the span with just what is inside.

3) Find certain nodes and completely remove them. Not having any problem finding the nodes in question and changing attributes but can't delete them. This codes seems like it should work but gives me an error...

Code: Select all

 
$elements = $doc->getElementsByTagName('*');
 
if (!is_null($elements)) {
  foreach ($elements as $element) {
    $NodeName = (string)$element->nodeName;
    print "Node: '$NodeName' \n";
    $attr = $element->getAttributeNode('class');
    if($attr){
      $class = (string)$attr->value;
      print "Class: '$class' \n";
      if($NodeName == 'span' AND $class == 'footscript'){
        $doc->removeChild($element);
      }
    }
  }
}
 
The error I get is...

DOMNode::removeChild() [function.DOMNode-removeChild]: Not Found Error in . . .

Any Ideas>
Post Reply