ajay600 Posted March 17, 2010 Share Posted March 17, 2010 i am comparing for text contents that are repeated in 2 web pages and removing the repeated contnt...after comparing for the similar elements , when i try to remove the repeated elements , i get an error as follows "Fatal error: Call to a member function removeChild() on a non-object in c:\wamp\www\b.php on line 38 "..i have shown the error line as bold in the code..how do i correct it $doc = new DOMDocument(); // An instance of DOMDocument @$doc->loadHTMLFile('http://www.web-source.net/web_design_tips/'); $doc2 = new DOMDocument(); // An instance of DOMDocument @$doc2->loadHTMLFile('http://www.web-source.net/html_codes_chart.htm'); $bold_text = array(); $bold_text2 = array(); $bold_elements = $doc->getElementsByTagName('b'); foreach($bold_elements as $element) $bold_text[] = $element->textContent; $bold_elements2 = $doc2->getElementsByTagName('b'); foreach($bold_elements2 as $element2) $bold_text2[] = $element2->textContent; $domElemsToRemove=array(); $srcBoth = array_intersect($bold_text , $bold_text2); foreach ($srcBoth as $img) { $domElemsToRemove[] = $img; } foreach( $domElemsToRemove as $img ) { $img->removeChild($img); } echo $doc->saveHTML(); Quote Link to comment https://forums.phpfreaks.com/topic/195596-error-in-removing-an-element/ Share on other sites More sharing options...
Psycho Posted March 17, 2010 Share Posted March 17, 2010 You are using it wrong. Check the manual: http://php.net/manual/en/domnode.removechild.php It should be: $parentObject->removeChild($childObject); Quote Link to comment https://forums.phpfreaks.com/topic/195596-error-in-removing-an-element/#findComment-1027769 Share on other sites More sharing options...
ajay600 Posted March 18, 2010 Author Share Posted March 18, 2010 ya ..u are right .... but there is another problem too ..right now i am only deleting only the variable ...but i should be removing the node instead .. i should create a DOMNodeList with matching text cotent .. how do i do it .. i did this to make A DOMNodeList of links with a matching href attribute using xpath and it works but i dont know how to create a DOMNodeList with matching text cotent ..please help foreach ($srcBoth as $src) { // Loops through the src strings that are common to both documents $imgs = $xpath->query('//a[@href="'.$src.'"]'); // A DOMNodeList of links with a matching href attribute foreach ($imgs as $img) { // Loops through the links $img->parentNode->removeChild($img); } Quote Link to comment https://forums.phpfreaks.com/topic/195596-error-in-removing-an-element/#findComment-1028209 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.