1.代码:
<?php
$str='<a href=""><img src="http://www.zhen.com/uploadfile/2012/1205/20121205112558940.jpg"></a><a href=""><img src="http://www.zhen.com/uploadfile/2012/1205/20121205112558940.jpg"></a>';
preg_match_all('/<img[^>]*src=[\'"]?([^>\'"\s]*)[\'"]?[^>]*>/i', $str, $out);
echo "<pre>";
print_r($out);
?>
2.结果:
<pre>Array
(
[0] => Array
(
[0] => <img src="http://www.zhen.com/uploadfile/2012/1205/20121205112558940.jpg">//匹配完整内容
[1] => <img src="http://www.zhen.com/uploadfile/2012/1205/20121205112558940.jpg">
)
[1] => Array
(
[0] => http://www.zhen.com/uploadfile/2012/1205/20121205112558940.jpg //只匹配()中规定的内容
[1] => http://www.zhen.com/uploadfile/2012/1205/20121205112558940.jpg
)
)
3.如下:
function spider_photos($str) {
$field = $GLOBALS['field'];
$_POST[$field.'_url'] = array();
preg_match_all('/<img[^>]*src=[\'"]?([^>\'"\s]*)[\'"]?[^>]*>/i', $str, $out);
$array = array();
if (isset($out[1]))foreach ($out[1] as $v) {
$_POST[$field.'_url'][] = $v;
}
return '1';
}