removing similar images

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
ajay600
Forum Newbie
Posts: 14
Joined: Tue Jan 19, 2010 8:54 am

removing similar images

Post by ajay600 »

the code identifies the images that are repeated in two web pages and then removes the repeated image from the web page ...
Can this coding be done in different way ..it doesnt matter if the new code is bigger than the existing code..
please help

Code: Select all

<?php   
$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');
 
$xpath = new DOMXPath($doc); // An instance of DOMXPath attatched to a DOMDocument
$imgList = $xpath->query('//img'); // A DOMNodeList containing all <img> tags
 
$xpath2 = new DOMXPath($doc2); // An instance of DOMXPath attatched to a DOMDocument
$imgList2 = $xpath2->query('//img'); // A DOMNodeList containing all <img> tags
 
$srcList = array(); // An array to hold src attribute strings
foreach ($imgList as $img) 
{ // Loops through the DOMNodeList of images
    $srcList[] = $img->getAttribute('src'); // Stores the src attribute of each image
}
$srcList2 = array(); // An array to hold src attribute strings
foreach ($imgList2 as $img2)
 { 
// Loops through the DOMNodeList of images
    $srcList2[] = $img2->getAttribute('src'); // Stores the src attribute of each image
}
 
$srcBoth = array_intersect($srcList, $srcList2);
 
foreach ($srcBoth as $src) { // Loops through the src strings that are common to both documents
    $imgs = $xpath->query('//img[@src="'.$src.'"]'); // A DOMNodeList of images with a matching src attribute
    foreach ($imgs as $img) { // Loops through the images
$img->parentNode->removeChild($img);
    }
}
echo $doc->saveHTML();
?>
Post Reply