removing similar images
Posted: Tue Feb 02, 2010 1:32 am
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
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();
?>