PHP通过正则表达式下载图片到本地的实现代码
程序员文章站
2022-04-09 09:23:30
复制代码 代码如下:
<?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){//循环取出每幅图的地址
$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;
}
?>
复制代码 代码如下:
<?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){//循环取出每幅图的地址
$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;
}
?>
上一篇: Get或Post提交值的非法数据处理
下一篇: phpBB BBcode处理的漏洞