2012-04-04 88 views
0

提取鏈接我想用PHP-preg_match_all()中提取圖像的從HTML頁面的路徑,該模式是如下使用preg_match_all

<img width="148" height="110" src="https://link1"> 
<img width="104" height="129" src="https://link2"> 
<img width="150" height="129" src="https://linkn"> 

我想在一個數組中的所有圖像的路徑。

回答

0

嘗試:

preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $html, $m); 
print_r($m); 

OR

preg_match_all("/<img .* src=\"([^']*?)\">/", $html, $m); 
echo "<pre>"; 
print_r($m[1]); 
+0

它輸出前陣一些HTML內容。 – 2012-04-04 04:51:22

+0

你可以使用第二個 – 2012-04-04 07:35:58

-1

您可以使用簡單的正則表達式來做到這一點,

用途:

preg_match_all('/<img .*?(?=src)src=\"([^\"]+)\"/si', $imglink, $result, PREG_PATTERN_ORDER); 

$ imglink是每個圖像的鏈接

and

$ result是數組中的結果。

與循環:

<?php 
$subject = array (
    '<img width="148" height="110" src="https://link1">' 
    , '<img width="104" height="129" src="https://link2">' 
    , '<img width="150" height="129" src="https://linkn">' 
); 

foreach ($subject as $imglink) { 
preg_match_all('/<img .*?(?=src)src=\"([^\"]+)\"/si', $imglink, $result, PREG_PATTERN_ORDER); 
$link[] = $result[1]; 
} 

echo "<pre>"; 
print_r ($link); 

?> 
-1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="tr" lang="tr" dir="ltr"> 
<head> 
<title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" /> 
<style type="text/css"> 
<!-- 
html,body,div,span,h1,h2,h3,p,hr,br,img,form,input,ul,li,a { 
margin:0; 
padding:0; 
border:0; 
} 
ul li {list-style:none;} 
body { 
font-family:Helvetica, Arial, Tahoma, sans-serif; 
font-size:13px; 
color:#444; 
line-height:1.5em; 
} 
#kapsayici { 
background:#fff; 
margin:10px auto; 
width:960px; 
border:0px solid #dfdfdf; 
min-height: 700px; 
} 
--> 
</style> 
</head> 
<body> 
<div id="kapsayici"> 
<?php 
$url = "http://www.mynet.com"; 
$icerik = file_get_contents($url); 

$resimler = array(); 
preg_match_all('/(img|src)=("|\')[^"\'>]+/i', $icerik, $veriler); 
$dizi=preg_replace('/(img|src)("|\'|="|=\')(.*)/i',"$3",$veriler[0]); 

foreach($dizi as $row) { 
    $bilgi = pathinfo($row); 
    if (isset($bilgi['extension'])) { 
     $bilgi['extension'] = strtolower($bilgi['extension']); 
     if (($bilgi['extension'] == 'jpg') || ($bilgi['extension'] == 'jpeg') || ($bilgi['extension'] == 'gif') || ($bilgi['extension'] == 'png')) array_push($resimler, $row); 
    } 
} 
$resimler=array_unique($resimler); 
echo "<ul>\n"; 
if (count($resimler)) { 
    $i_count=0; 
    foreach($resimler as $resim) { 
     $i_count++; 
     echo "<li><img src=\"{$resim}\" /></li>\n"; 
    } 

} 
echo "</ul>\n"; 
?> 
</div> 
</body> 
</html> 
+1

請加一些解釋以便更好地理解。 – akjoshi 2013-01-18 11:33:41