0

I am making a script which works individually but I am looking for all details of corresponding image like alt, title and image src details. I have written this:

$websitelink='http://www.wallsave.com/wallpaper/1920x1200/houses-colorful-walpapers-history-156851.html';
$str = file_get_contents($websitelink);
if(strlen($str)>0) {
    preg_match_all("/< *img[^>]*src *= *[\"\']?([^\"\']*)/i",$str,$src);
    preg_match_all("/< *img[^>]*alt *= *[\"\']?([^\"\']*)/i",$str,$alt);
    preg_match_all("/< *img[^>]*title *= *[\"\']?([^\"\']*)/i",$str,$title);

    foreach ($src[1] as $srci) {
        $imgsrc=$srci.'</br>';
    }

    foreach ($alt[1] as $alti) {
        $altimg=$alti.'</br>';
    }

    foreach ($title[1] as $titlei) {
        $titleimg= $titlei.'</br>';
    }


    $alt_title_src=$altimg.$imgsrc.$titleimg.'</br>';
    echo $alt_title_src;
}

It does not work any idea or xpath will be better.

chanchal118
  • 3,551
  • 2
  • 26
  • 52
Priya
  • 165
  • 1
  • 12

2 Answers2

0

The best way I would recommend to do this is via DOMDocument. Here is my code using your final variable names for clarity:

// Set the URL value.
$url = 'http://www.wallsave.com/wallpaper/1920x1200/houses-colorful-walpapers-history-156851.html';

// Get the contents of the HTML file & load it into a variable.
$html = file_get_contents($url);

// Parse the HTML with 'DOMDocument()'
$dom = new DOMDocument();
@$dom->loadHTML($html);

// Get all the image tags.
$imgs = $dom->getElementsByTagName('img')->item(0);

$alt_title_src_array = array();
foreach ($dom->getElementsByTagName('img') as $values) {

  // Get the image 'src' value.
  $imgsrc = $values->attributes->getNamedItem("src")->value;

  // Get the image 'alt' value.
  $altimg = $values->attributes->getNamedItem("alt")->value;

  // Get the image 'title' value.
  $titleimg = $values->attributes->getNamedItem("title")->value;

  $alt_title_src_array[] = $altimg . $imgsrc . $titleimg . '</br>';

}

// Dump the '$alt_title_src_array' for debugging.
echo '<pre>';
print_r($alt_title_src_array);
echo '</pre>';

The output I get when running this is:

Array
(
    [0] => http://www.wallsave.com/thumb/houses-colorful-walpapers-history-156851.jpg

    [1] => Follow Me on Pinteresthttp://passets-cdn.pinterest.com/images/follow-on-pinterest-button.png

    [2] => http://platform.tumblr.com/v1/share_1.png

    [3] => Wallpapers Houses Colorful Walpapers History   1920x1200http://www.wallsave.com/wallpapers/1920x1200/houses/156851/houses-colorful-walpapers-history-156851.jpgHouses Colorful Walpapers History   Wallpaper

    [4] => http://www.wallsave.com/thumb/houses-d-house-nature-hd-jootix-279025.jpg

    [5] => http://www.wallsave.com/thumb/houses-in-the-village-nature-landscape-and-996677.jpg

    [6] => http://www.wallsave.com/thumb/houses-sea-villas-hd-942695.jpg

    [7] => http://www.wallsave.com/thumb/houses-tree-house-beautiful-picture-259493.jpg

    [8] => http://www.wallsave.com/thumb/houses-of-parliament-and-big-ben-london-city-pictures-593315.jpg

    [9] => http://www.wallsave.com/thumb/houses-taipei-house-paper-images-buildings-610526.jpg

    [10] => http://www.wallsave.com/thumb/houses-wood-house-mountain-and-lake-895032.jpg

    [11] => http://www.wallsave.com/thumb/houses-man-red-house-sidewalk-snow-trees-winter-us-811843.jpg

    [12] => http://www.wallsave.com/thumb/houses-dr-house-63348.jpg

    [13] => http://www.wallsave.com/thumb/houses-of-parliament-london-united-kingdom-city-121730.jpg

    [14] => http://www.wallsave.com/thumb/houses-colorful-walpapers-history-156851.jpg

    [15] => http://www.wallsave.com/thumb/houses-village-for-free-1876649.jpg

)
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • which will parse data faster preg_replace_all or dom. for me time is very important so i was going for preg – Priya Jan 01 '14 at 05:08
  • Seems fast enough to me. Linked to another article that addresses your issue head-on. http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php – Giacomo1968 Jan 01 '14 at 05:10
0

Try this:

<?php
$url = 'http://www.wallsave.com/wallpaper/1920x1200/houses-colorful-walpapers-history-156851.html';

// Get the contents of the HTML file & load it into a variable.
$str = file_get_contents($url);

if(strlen($str)>0) {
    preg_match_all("/< *img[^>]*src *= *[\"\']?([^\"\']*)/i",$str,$src);
    preg_match_all("/< *img[^>]*alt *= *[\"\']?([^\"\']*)/i",$str,$alt);
    preg_match_all("/< *img[^>]*title *= *[\"\']?([^\"\']*)/i",$str,$title);

    foreach ($src[1] as $srci) {
        $imgsrc[] =$srci;
    }

    foreach ($alt[1] as $alti) {
        $altimg[]=$alti;
    }

    foreach ($title[1] as $titlei) {
        $titleimg[] = $titlei;
    }

for($i=0;$i < count($src[1] );$i++){
    echo "src :".$imgsrc[$i]." Alt :".$altimg[$i]." Title :".$titleimg[$i]."<br/>";
}

}

?>

OUTPUT:

src :http://www.wallsave.com/thumb/houses-colorful-walpapers-history-156851.jpg Alt :Follow Me on Pinterest Title :Houses Colorful Walpapers History Wallpaper
src :http://passets-cdn.pinterest.com/images/follow-on-pinterest-button.png Alt :Wallpapers Houses Colorful Walpapers History 1920x1200 Title :
src :http://platform.tumblr.com/v1/share_1.png Alt : Title :
src :http://www.wallsave.com/wallpapers/1920x1200/houses/156851/houses-colorful-walpapers-history-156851.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-d-house-nature-hd-jootix-279025.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-in-the-village-nature-landscape-and-996677.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-sea-villas-hd-942695.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-tree-house-beautiful-picture-259493.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-of-parliament-and-big-ben-london-city-pictures-593315.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-taipei-house-paper-images-buildings-610526.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-wood-house-mountain-and-lake-895032.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-man-red-house-sidewalk-snow-trees-winter-us-811843.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-dr-house-63348.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-of-parliament-london-united-kingdom-city-121730.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-colorful-walpapers-history-156851.jpg Alt : Title :
src :http://www.wallsave.com/thumb/houses-village-for-free-1876649.jpg Alt : Title :
Awlad Liton
  • 9,366
  • 2
  • 27
  • 53