error in removing an element

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
ajay600
Forum Newbie
Posts: 14
Joined: Tue Jan 19, 2010 8:54 am

error in removing an element

Post by ajay600 »

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

Code: Select all

 
$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 )
{ 
 
[u][b]$img->removeChild($img)[/b];[/u]
 
} 
echo $doc->saveHTML();
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: error in removing an element

Post by Sofw_Arch_Dev »

Seems like $img is trying to remove itself as a child. Is this what you intend?
ajay600
Forum Newbie
Posts: 14
Joined: Tue Jan 19, 2010 8:54 am

Re: error in removing an element

Post by ajay600 »

$srcboth has all the repeated text content .. now i would like to remove all the repeated text content from the web page .. using a foreach loop if i try to remove each of the elements i get this error "Fatal error: Call to a member function removeChild() on a non-object in c:\wamp\www\b.php on line 38 " ..how to remove the elements from the web page
User avatar
Sofw_Arch_Dev
Forum Commoner
Posts: 60
Joined: Tue Mar 16, 2010 4:06 pm
Location: San Francisco, California, US

Re: error in removing an element

Post by Sofw_Arch_Dev »

I see that Ajay. But your code is removing an element from itself. Look at the line you've bolded.
ajay600
Forum Newbie
Posts: 14
Joined: Tue Jan 19, 2010 8:54 am

Re: error in removing an element

Post by ajay600 »

i found the problem but i dont know how to correct it

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 using xpath so that the node can be removed using remove child how do i do it ..

i tried using THIS but this did'nt work

$imgs = $xpath->query('//b="$img");

how can i correct this line

..please help
Post Reply