0

我想使用 PHP-preg_match_all() 从 html 页面中提取图像的路径,模式如下

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

我想要一个数组中的所有图像路径。

4

3 回答 3

0

尝试:

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

或者

preg_match_all("/<img .* src=\"([^']*?)\">/", $html, $m);
echo "<pre>";
print_r($m[1]);
于 2012-04-04T04:43:03.300 回答
-1

您可以使用简单的正则表达式来做到这一点,

利用:

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

$imglink 是您的每个图片链接

$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);

 ?>
于 2012-04-04T05:19:23.340 回答
-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>
于 2012-09-22T09:54:41.067 回答