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

Php preg_match_all打印图像文件的所有alt、title和src,php,xpath,Php,Xpath,我正在制作一个单独工作的脚本,但我正在查找相应图像的所有详细信息,如alt、title和image src详细信息。我写过这样的话: $websitelink='http://www.wallsave.com/wallpaper/1920x1200/houses-colorful-walpapers-history-156851.html'; $str = file_get_contents($websitelink); if(strlen($str)>0) { preg_matc

我正在制作一个单独工作的脚本,但我正在查找相应图像的所有详细信息,如
alt
title
image 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;
}
$websitelink=”http://www.wallsave.com/wallpaper/1920x1200/houses-colorful-walpapers-history-156851.html';
$str=文件获取内容($websitelink);
如果(strlen($str)>0){
preg\u match\u all(“/<*img[^>]*src*=*[\'\']]?([^\'\']*)/i“,$str,$src);
preg\u match\u all(“/<*img[^>]*alt*=*[\'\']]?([^\'\']*)/i“,$str,$alt);
preg\u match\u all(“/<*img[^>]*title*=*[\'\']]?([^\'\']*)/i”,$str,$title);
foreach($src[1]作为$srci){
$imgsrc=$srci.
'; } foreach($alt[1]作为$alti){ $altimg=$alti'
'; } foreach($title[1]为$titlei){ $titleimg=$titlei'
'; } $alt_title_src=$altimg.$imgsrc.$titleimg.
'; echo$alt\u title\u src; }

它不起任何作用,否则xpath会更好

我建议最好的方法是通过。为了清晰起见,以下是我使用最终变量名的代码:

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

试试这个:

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 :

您应该使用DOMDocument()@AwladLiton我不想使用getelementbytagname和get_头,它们非常慢,所以我选择pregI。我想您没有从url获取数据。它显示禁止。我已经用示例数据进行了测试,您的代码正在运行。测试:可能重复@AwladLiton是的,我看到了,但在我的服务器中,它的输出将更快地解析数据preg_替换_all或dom。对我来说,时间很重要,所以我要去的地方对我来说似乎足够快。链接到另一篇直接解决您的问题的文章。
<?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 :