Jump to content

Recommended Posts

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();

Link to comment
https://forums.phpfreaks.com/topic/195596-error-in-removing-an-element/
Share on other sites

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);
}

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.