Tips->Programmazione->Php->



Back


  • Average 0.00 of 0 votes
Average 0.00 of 0 votes 0 votes


Estrarre immagini e links in php 5 da una pagina web senza parsing usando il Dom




Usiamo il Dom

(Error rendering Element: ads/onlylink468_60)	

<?
$dom = new domdocument;
$baseurl = 'http://www.pierpaoloromanelli.com';
$url ="http://www.pierpaoloromanelli.com"  ; 
@$dom->loadHTMLFile($url);
//Estrarre il titolo
//echo $dom->getElementsByTagName("title")->item(0)->textContent;;
/*
$anchor = $dom->getElementsByTagName('img');

foreach ($anchor as $node) {
echo "c";
$anchor_href=$node->getAttribute('src');
$anchor_text=$node->textContent;
print $anchor_href;
}
*/

$xpath = new domxpath($dom);
$xNodes = $xpath->query('//img[@src]');
    foreach ($xNodes as $xNode)
    {
        $imgsrc = $xNode->getAttribute('src');
        $arrIgnore = array(
               'immagine1.gif',
                'immagine2.gif'
                );
        $doIgnore = $imgsrc == '';
        foreach ($arrIgnore as $sIgnore)
        {
            if ( !(strpos($imgsrc, $sIgnore) === false) )
            {
                $doIgnore = true;
                break;
            }
        }
        if (!$doIgnore)
        {
           $href = $xNode->parentNode->getAttribute('href');
            echo '<a href="' .
                    htmlentities($href) . '">';
            echo '<img src="' .
                    htmlentities ($imgsrc) .
                    '" alt="[Link]" />';
            echo '</a> &nbsp; ';
        }
    }
?>

 
(Error rendering Element: ads/onlylink468_60)





Estrarre immagini e links in php 5 da una pagina web senza parsing usando il Dom




Non perdere le incredibili novità! Iscriviti immediatamente!



Inserisci Annuncio Gratis

segnala sito

Postato da: Pierpaolo Romanelli




0 Comments +
Comments:
Add your comment:    

   

   

   

   

Technorati Profile