用php实现的下载css文件中的图片的代码

yipeiwu_com6年前PHP代码库
作为一个资深并且专业的扒皮人员,在我从初三开始投入伟大的互联网中到现在积累了丰富的扒皮经验。我相信每个做web的程序员也都会有类似的经历。

在扒皮过程中,必不可少的需要下载样式文件中的图片。碰到比较庞大的样式文件,其中可能会有上百个需要下载的图片,那么使用下面这段小代码是最为合适的了。
复制代码 代码如下:

< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.

Author: kimi
Documentation: 下载样式文件中的图片,水水专用扒皮工具
*/

//note 设置PHP超时时间
set_time_limit(0);

//note 取得样式文件内容
$styleFileContent = file_get_contents('images/style.css');

//note 匹配出需要下载的URL地址
preg_match_all("/url\((.*)\)/", $styleFileContent, $imagesURLArray);

//note 循环需要下载的地址,逐个下载
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}

以上是转载的原文,下面是修改版本,转载的话请留个链接。

复制代码 代码如下:

<?php
set_time_limit ( 0 );
$styleFileContent = file_get_contents ( 'http://img.jb51.net/skin/newblue/main.css' );
preg_match_all ( "/url\((.*)\)/", $styleFileContent, $imagesURLArray );
$imagesURLArray = array_unique ( $imagesURLArray [1] );
foreach ( $imagesURLArray as $imagesURL ) {
$dir=dirname($imagesURL);
if(!file_exists($dir))
{
//创建目录
createDir($dir);
}
$imagesURL='//www.jb51.net/'.$imagesURL;
file_put_contents ( basename ( $imagesURL ), file_get_contents ( $imagesURL ) );
}

function createDir($path) {
$path = str_replace('\\','/',$path) ;
if ( is_dir($path) ) return true ;
if ( file_exists($path) ) return false ;

$parent = substr($path ,0, strrpos($path,'/') ) ;
if ( $parent==='' || $parent==='.' || createDir( $parent ) )
return @mkdir($path) ;
else return false ;
}
?>

相关文章

php使用CURL伪造IP和来源实例详解

本文实例讲述了php使用CURL伪造IP和来源的方法。分享给大家供大家参考。具体分析如下: 伪造IP来源对于php来说是很简单的一件事情,我们只要利用了php的curl即可实现伪造IP来...

php Redis函数用法实例总结【附php连接redis单例类】

本文实例总结了php Redis函数用法。分享给大家供大家参考,具体如下: 一直在拿PHP使用Redis,但是总感觉不牢靠,索性借这个时间空余一气呵成, 把PHP中所有操作到的Redis...

php的memcached客户端memcached

memcache的官方主页:http://pecl.php.net/package/memcachememcached的官方主页:http://pecl.php.net/package/...

php进行ip地址掩码运算处理的方法

本文实例讲述了php进行ip地址掩码运算处理的方法。分享给大家供大家参考,具体如下: ip解析: function ip_parse($ip_str) { $mark_len =...

PHP实现数据四舍五入的方法小结【4种方法】

本文实例总结了PHP实现数据四舍五入的方法。分享给大家供大家参考,具体如下: 在PHP开发中,有时候我们会遇到将数据进行四舍五入的运算情况,本文分享了用PHP实现数据四舍五入的4种方法。...