php获取字符串中第一张图片正则表达式

 1 <?php
 2     $test = '<p><a href="aa.jpg">444</a><img height="768" width="1024" alt="" src="/uploadfiles/28/Tree.jpg" /></p>
 3     <p>444<img height="768" width="1024" alt="" src="http://www.abcdefg.com/uploadfiles/sf/Tree.jpg" /></p>
 4     fsdafasdfasdfasdf
 5     <p>444<img height="768" width="1024" alt="" src="/uploadfiles/28/elm.jpg" /></p>sdfasdfasdf<p>
 6     <p>444<img height="768" width="1024" alt="" src="/uploadfiles/28/maple.jpg" /></p>dgdgrfdgdf
 7     <p>444<img height="768" width="1024" alt="" src="/uploadfiles/40/Tree.jpg" /></p>';
 8  
 9 //方法一:
10   preg_match_all("/src="/?(.*?)"/",$test,$match);
11   //print_r($match[1]);
12   echo($match[1][0]);
13  
14 //方法二:
15   preg_match ("<img.*src=["](.*?)["].*?>",$test,$match);
16   echo "$match[1]";
17 
18 ?>

 

posted @ 2016-03-31 12:03  半醉浪子  阅读(459)  评论(0)    收藏  举报