PHP 正则匹配下载网络资源

<?php
/**
 * 匹配下载网络资源
 */
header("Content-type: text/html;charset=utf-8");
error_reporting(E_ALL ^E_NOTICE^E_WARNING);
class DownloadFileFromWebsite{
	private $img_ext_arr=array('WEBP','BMP','JPG','GIF','JPEG','PSD','EPS','PNG','RAW','EMF','ICO');
	public  $file_dir;
	public  $matches_x;
	function __construct($web_url,$root_dir){
       if(empty($web_url) || empty($root_dir)){
		   exit('参数错误!');
	   }
       $this->file_dir=$this->creatDirByWebUrl($web_url,$root_dir);
	   $this->matches_x=$this->pregMatchUrl($web_url);
    }
	
	//通过地址获取文件内容
	protected  function getContentByUrl($url,$param_arr)
	{
		$param_sring=http_build_query($param_arr);
		return file_get_contents($url.$param_sring);
	}
	//下载文件
	protected function dowmload_file($file_url, $save_to,$logDir,$encodeName)
	{
		$file_name=basename($file_url);
		$content = file_get_contents($file_url);
		$result=file_put_contents($save_to, $content);
		if($result){
			$status='下载成功!';
			$state='';
		}else{
			unlink($this->file_dir.'/img/'.$file_name);//删除空的错误文件
			$status='下载失败!!!';
			$state='[✘]';
		}
		//根据系统进行配置
		$encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8';
		$arr_name_ext=explode('.',basename($save_to));
		$filename=iconv($encode,'UTF-8',$encodeName);
		$ext=$arr_name_ext[1];
		$log_record=$state.'图片文件:'.$file_name.'   ------  ['.$filename.'.'.$ext.']   ------   '.date('Y-m-d H:i:s',time()).' ------ 大小:'.round($result/1024,2).'kb ------'.$status.PHP_EOL;
		echo $log_record.'<br>';
		file_put_contents($logDir,$log_record,FILE_APPEND);
	}
	//根据访问地址建立文件夹目录
	private function creatDirByWebUrl($url,$root_dir){
		preg_match_all('/([http|https]*):\/\/*(.*?\/.*)/',$url,$match_web_url);
		$dir_path=str_replace('/','_',$match_web_url[2][0]);
		$this->file_dir=$root_dir.'/'.$match_web_url[1][0]."_".$dir_path;
		if(!is_dir($this->file_dir.'/img')){
			$staue=mkdir($this->file_dir.'/img',0777,true);
			if(!$staue){
				echo $dir_path.'目录创建失败!<br/>';
				return false;
			}
		}
		return $this->file_dir;
	}
	//根据设定规则,匹配要下载的资源
	public function pregMatchUrl($url){
		$param_arr=array();
		$string_html=$this->getContentByUrl($url,$param_arr);
		preg_match_all('/<img src=["|\']([http|https].*)["|\'] alt=["|\'](.*)["|\'].*\/*?>/U',$string_html,$matches_x);	
		return $matches_x;
	}
	
	//处理文件后缀,并下载资源
	public function renameDownloadFiles(){
		foreach($this->matches_x[1] as $k=>$v){
			$arr=explode('.',basename($v));
			$ext=strtoupper(end($arr));
			//根据系统进行配置
			$encode = stristr(PHP_OS, 'WIN') ? 'GBK' : 'UTF-8';
			$this->matches_x[2][$k] = iconv('UTF-8', $encode, $this->matches_x[2][$k]);
			if(in_array($ext,$this->img_ext_arr)){
				$this->dowmload_file($v,$this->file_dir.'/img/'.$this->matches_x[2][$k].'.'.strtolower($ext),$this->file_dir.'/log.txt',$this->matches_x[2][$k]);
			}else{
			   //处理特殊后缀,排除网站后缀干扰  如 jpg!|  jpg@90
				$str_ext='['.join('|',$this->img_ext_arr).']{1,}';
				preg_match_all('/'.$str_ext.'/',strtoupper($ext),$match_all);
				if($match_all){
					$ext_new=$match_all[0][0];
				}
				$this->dowmload_file($v,$this->file_dir.'/img/'.$this->matches_x[2][$k].'.'.strtolower($ext_new),$this->file_dir.'/log.txt',$this->matches_x[2][$k]);
			}
		}
	}
}
//$web_url='http://www.duok******.com/';
$pageCount=20;
for($a=1;$a<=$pageCount;$a++){
	$web_url='http://www.duok*****.com/list/1-'.$a;
	$root_dir='./DuoKan_DownloadFile';
	$obj=new DownloadFileFromWebsite($web_url,$root_dir);
	$obj->renameDownloadFiles();
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值