最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP通過(guò)正則表達(dá)式下載圖片到本地的實(shí)現(xiàn)代碼

 更新時(shí)間:2011年09月19日 17:17:29   作者:  
PHP通過(guò)正則表達(dá)式下載圖片到本地的實(shí)現(xiàn)代碼,主要是通過(guò)正則表達(dá)式匹配網(wǎng)頁(yè)內(nèi)容中的圖片地址并下載。
復(fù)制代碼 代碼如下:

<?php
/*
author: ssh_kobe
date: 20110602
shortage: 如果網(wǎng)頁(yè)中的圖片路徑不是絕對(duì)路徑,就無(wú)法抓取
*/
set_time_limit(0);//抓取不受時(shí)間限制

$URL='http://pp.baidu.com/';//任意網(wǎng)址

get_pic($URL);

function get_pic($pic_url) {
//獲取圖片二進(jìn)制流
$data=CurlGet($pic_url);
/*利用正則表達(dá)式得到圖片鏈接*/
$pattern_src = '/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/';
$num = preg_match_all($pattern_src, $data, $match_src);
$arr_src=$match_src[1];//獲得圖片數(shù)組
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){//循環(huán)取出每幅圖的地址
$num = preg_match_all($pattern_type, $pic_item, $match_type);
$pic_name = get_unique().$match_type[1][0];//改時(shí)微秒時(shí)間戳命名
//以流的形式保存圖片
$write_fd = @fopen($pic_name,"wb");
@fwrite($write_fd, CurlGet($pic_item));
@fclose($write_fd);
echo "[OK]..!";
}
return 0;
}

//通過(guò)微秒時(shí)間獲得唯一ID
function get_unique(){
list($msec, $sec) = explode(" ",microtime());
return $sec.intval($msec*1000000);
}

//抓取網(wǎng)頁(yè)內(nèi)容
function CurlGet($url){
$url=str_replace('&amp;','&',$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;
}
?>

相關(guān)文章

最新評(píng)論

嘉峪关市| 绵竹市| 巫溪县| 宜都市| 黄冈市| 泊头市| 电白县| 栾城县| 岱山县| 金门县| 陆川县| 武鸣县| 通渭县| 波密县| 梅河口市| 乌兰浩特市| 通化市| 綦江县| 崇明县| 香河县| 麻江县| 兴和县| 恭城| 梧州市| 宁国市| 宜兰市| 周口市| 满洲里市| 湘西| 剑川县| 延津县| 武义县| 万源市| 普兰店市| 华坪县| 荆州市| 浪卡子县| 延吉市| 富蕴县| 彝良县| 海门市|