php中通過Regex下載內容中的遠程圖片的函數代碼_php技巧

來源:互聯網
上載者:User
這段程式其實是屬於“小偷程式”的重要部分。 這一段程式只是下載遠程圖片的那一段而已,程式寫的比較簡單,大多部分都做了注釋。
複製代碼 代碼如下:

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)){ //擷取到檔案就執行下面的操作
$dir = "upload/".date("Ymd")."/";//指定新的儲存路徑
if (!file_exists($dir)){//判斷目錄是否存在
@mkdir($dir,511,true); //建立多級目錄,511轉換成十進位是777具有可執行許可權
}
$nfn = $dir.date("Ymdhis").$i.".".$filetype; //構建檔案的新名字
$nf = @fopen($nfn,"w"); //建立檔案
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通過Regex下載圖片到本地的實現代碼
複製代碼 代碼如下:

<?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);
/*利用Regex得到圖片連結*/
$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;
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.