你看一下 preg_match_all 的文档,它会返回匹配的次数,你直接用这个次数就可以获取到最后一个匹配:
<?php
$article = <<<EOT
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<img src="//
cdn.v2ex.co/avatar/2cd8/7697/76017_large.png?m=1412760421" class="avatar" border="0" align="default">
<img src="//
cdn.v2ex.co/avatar/2cd8/7697/76017_large.png?m=1412760422" class="avatar" border="0" align="default">
<img src="//
cdn.v2ex.co/avatar/2cd8/7697/76017_large.png?m=1412760423" class="avatar" border="0" align="default">
<img src="//
cdn.v2ex.co/avatar/2cd8/7697/76017_large.png?m=1412760424" class="avatar" border="0" align="default">
</div>
</body>
</html>
EOT;
$pattern = '/<img[^>]+src\\s*=\\s*[\'\"]([^\'\"]+)[\'\"][^>]*>/im';
// 设置 flag 为 PREG_SET_ORDER
if ($match = preg_match_all($pattern, $article, $matches, PREG_SET_ORDER)) {
var_dump($matches[--$match]);
}
// 不设置 PREG_SET_ORDER
if ($match = preg_match_all($pattern, $article, $matches)) {
var_dump($matches[0][--$match]);
}