如何使用以下脚本获取多个图像
<?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]." />";
}
?>
请帮忙!
答案 0 :(得分: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');
}