2013-06-27 42 views
-1

我得到多張圖片我如何可以使用下面的腳本如何使用URL

<?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

我建議使用['DOMDocument'](http://php.net/manual/en/class.domdocument.php)解析HTML,verus使用字符串搜索。 –

回答

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'); 
} 
+0

完美的工作很好..非常感謝你的幫助。其他開發人員也花時間給我。謝謝。 – user2528747

+0

@ user2528747:不客氣! :-) –