Home > php教程 > php手册 > php不会正则的痛

php不会正则的痛

WBOY
Release: 2016-06-06 19:33:35
Original
1260 people have browsed it

不会正则下载暴漫好痛苦,唯一的正则是网上找的,最后还是用explode来处理了,小弟新人,求大牛改正。 无 ?php/** * 正则表达式匹配需要的图片 * @return ArrayIterator $arr */function pregIMG($str){//$str ='a href="/articles/7442969" target="_blank"i

不会正则下载暴漫好痛苦,唯一的正则是网上找的,最后还是用explode来处理了,小弟新人,求大牛改正。
<?php
/**
 * 正则表达式匹配需要的图片
 * @return ArrayIterator $arr
 */
function pregIMG($str){
	//$str ='<a href="/articles/7442969" target="_blank"><img alt="没标题" src="http://www.68idc.cn/help/uploads/allimg/160322/2016161356-0.jpg"   style="max-width:90%" /></a>';
	$pattern='/<img .+src=\"(.+\.jpg)\"?.+ alt="php不会正则的痛" >/i';
	preg_match_all($pattern,$str,$matchs);
	return $matchs;
}
/**
 * 查找某个页面的jpg
 */
function getIMG($url,$dir){

	if (empty($url)) return false;
	$contents = file_get_contents($url);
	$data = pregIMG($contents);
	$jpgs = $data[1];
	
	unset($data);
	
	$data = array();
	foreach ($jpgs as $k => $v){
		$t = explode('/', $v);
		if ($t[3] == 'large'){
			$data[$k] = $v;
		}
	}

	if(!file_exists($dir) || !is_dir($dir)){
		mkdir($dir,0777);
	}
	$num = 1;
	foreach ($data as $v){
		copy($v, $dir.'/'.time(). rand(10,100).'.jpg');
		usleep(1);
		$num++;
	}
	return $num;
}
function getBaoZou($page=1){
	$url = "http://baozoumanhua.com/tucao/fresh/page/".$page."?sv=1395827058";
	$dir = "public";
	return getIMG($url, $dir);
}
function getMoreBaoZou($page){
	//下载前10页
	$num = 0;
	for ($i = 1;$i < $page; $i++){
		$num += getBaoZou($i);
	}
	echo '一共下载了'.$num.'张暴走漫画';
}
getMoreBaoZou(10);//获取前10页的暴走漫画
?>
Copy after login
php不会正则的痛
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template