php中通過正則表達式下載內容中的遠程圖片的函數代碼
更新時間:2012年01月10日 12:01:30 投稿:mdxy-dxy
下午抽空寫了個用PHP正則表達式判斷內容中的圖片,下載并保存非本域名下的圖片的程序
這段程序其實是屬于“小偷程序”的重要部分。 這一段程序只是下載遠程圖片的那一段而已,程序寫的比較簡單,大多部分都做了注釋。
if (preg_match_all("/http://[^ "']+[.jpg|.gif|.jpeg|.png]+/ui",stripcslashes($content),$aliurl)){ $i=0; //多個文件++ while(list($key ,$v) = each($aliurl[0])){ //echo $v."<br />"; $filetype = pathinfo($v, PATHINFO_EXTENSION); //獲取后綴名 $ff = @file_get_contents($v); //獲取2進制文件內容 if(!stripos($v,"jb51.net")){//判斷是否是自己網站下的圖片 if (!empty($ff)){ //獲取到文件就執(zhí)行下面的操作 $dir = "upload/".date("Ymd")."/";//指定新的存儲路徑 if (!file_exists($dir)){//判斷目錄是否存在 @mkdir($dir,511,true); //創(chuàng)建多級目錄,511轉換成十進制是777具有可執(zhí)行權限 } $nfn = $dir.date("Ymdhis").$i.".".$filetype; //構建文件的新名字 $nf = @fopen($nfn,"w"); //創(chuàng)建文件 fwrite($nf,$ff); //寫入文件 fclose($nf); //關閉文件 $i++; //多文件++ echo "<img src="".$nfn."">"; $content = str_replace($v,$nfn, $content);//替換content中的參數 }else{//獲取不到圖片則替換為默認圖片 $content = str_replace($v,"/upload/201204/20120417213810742.gif", $content);//替換content中的參數 } } } }
PHP通過正則表達式下載圖片到本地的實現(xiàn)代碼
<?php /* author: ssh_kobe date: 20110602 shortage: 如果網頁中的圖片路徑不是絕對路徑,就無法抓取 */ 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){//循環(huán)取出每幅圖的地址 $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; } ?>
下面查看實例
function get_pic_url($content){ $pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg]))[\'|\"].*?[\/]?>/";//正則 preg_match_all($pattern,$content,$match);//匹配圖片 return $match[1];//返回所有圖片的路徑 }
以上就是php中通過正則表達式下載內容中的遠程圖片的函數代碼的詳細內容,更多關于正則表達式下載內容中的遠程圖片的資料請關注腳本之家其它相關文章!
相關文章
php實現(xiàn)從上傳文件創(chuàng)建縮略圖的方法
這篇文章主要介紹了php實現(xiàn)從上傳文件創(chuàng)建縮略圖的方法,涉及php操作上傳文件及圖片操作的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04php curl發(fā)起get與post網絡請求案例詳解
這篇文章主要介紹了php curl發(fā)起get與post網絡請求案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-09-09