php获取内容第一张图片

<?php
    $test = '<p><a href="aa.jpg">444</a><img height="768" width="1024" alt="" src="/uploadfiles/28/Tree.jpg" /></p>
    <p>444<img height="768" width="1024" alt="" src="http://www.abcdefg.com/uploadfiles/sf/Tree.jpg" /></p>
    fsdafasdfasdfasdf
    <p>444<img height="768" width="1024" alt="" src="/uploadfiles/28/elm.jpg" /></p>sdfasdfasdf<p>
    <p>444<img height="768" width="1024" alt="" src="/uploadfiles/28/maple.jpg" /></p>dgdgrfdgdf
    <p>444<img height="768" width="1024" alt="" src="/uploadfiles/40/Tree.jpg" /></p>';

 

//方法一:

 preg_match_all("/src=\"\/?(.*?)\"/",$test,$match);
  //print_r($match[1]);
  echo($match[1][0]);

 

//方法二:

  preg_match ("<img.*src=[\"](.*?)[\"].*?>",$test,$match);
  echo "$match[1]";


?>

PHP通过正则表达式下载图片到本地的实现代码 

代码如下:

<?php 
 
set_time_limit(0);//抓取不受时间限制 

$URL='http://pp.baidu.com/';//任意网址 

get_pic($URL); 

function get_pic($pic_url) { 
//获取图片二进制流 
$data=CurlGet($pic_url); 
 
$pattern_src = '/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/'; 
$num = preg_match_all($pattern_src, $data, $match_src); 
$arr_src=$match_src[1];//获得图片数组 
get_name($arr_src); 

echo "<br>finished!!!"; 
return 0; 


 
function get_name($pic_arr) 

//图片类型 
$pattern_type = '/(/.(jpg|bmp|jpeg|gif|png))/'; 

foreach($pic_arr as $pic_item){//循环取出每幅图的地址 
$num = preg_match_all($pattern_type, $pic_item, $match_type); 
$pic_name = get_unique().$match_type[1][0];//改时微秒时间戳命名 
//以流的形式保存图片 
$write_fd = @fopen($pic_name,"wb"); 
@fwrite($write_fd, CurlGet($pic_item)); 
@fclose($write_fd); 
echo "[OK]..!"; 

return 0; 


//通过微秒时间获得唯一ID 
function get_unique(){ 
list($msec, $sec) = explode(" ",microtime()); 
return $sec.intval($msec*1000000); 


//抓取网页内容 
function CurlGet($url){ 
$url=str_replace('&','&',$url); 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_HEADER, false); 

//curl_setopt($curl, CURLOPT_REFERER,$url); 
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)"); 
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); 
$values = curl_exec($curl); 
curl_close($curl); 
return $values; 

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值