preg_match_all打印图像文件的所有alt,title和src

时间:2014-01-01 04:41:59

标签: php xpath

我正在创建一个单独运行的脚本,但我正在查找相应图片的所有详细信息,例如alttitleimage src详细信息。我写了这个:

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

它不起作用或xpath会更好。

2 个答案:

答案 0 :(得分:0)

我建议这样做的最好方法是通过DOMDocument。为清晰起见,这是使用最终变量名称的代码:

// 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>';

运行时得到的输出是:

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

)

答案 1 :(得分:0)

试试这个:

<?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/>";
}

}

?>

<强>输出:

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 :