-1

如何使用以下脚本获取多个图像

<?php
$url = 'http://stackoverflow.com/questions/7994340/how-can-i-get-image-from-url-in-php-or-jquery-or-in-both';
$data = file_get_contents($url);

if(strpos($data,"<img"))
{
    $imgpart1 = explode('<img src=',$data);
    $imgpart2 = explode('"',$imgpart1[1]);
    echo "<img src=".$imgpart2[1]." />";
}
?>

请帮忙!

4

1 回答 1

0

您想为此使用 HTML/DOM 解析器,这不是正则表达式或字符串搜索的工作。

我喜欢 PHP 的DOMDocument,它不是太难用。

$url = 'http://stackoverflow.com/questions/7994340/how-can-i-get-image-from-url-in-php-or-jquery-or-in-both';
$data = file_get_contents($url);

$dom = new DOMDocument;
// I usually say to NEVER use the "@" operator,
// but everyone's HTML isn't perfect, and this may throw warnings
@$dom->loadHTML($data);

$img = $dom->getElementsByTagName('img');
foreach($img as $x){
    echo $x->getAttribute('src');
}
于 2013-06-27T15:36:52.270 回答