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

yipeiwu_com5年前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判断类是否存在函数class_exists用法分析

本文实例分析了php判断类是否存在函数class_exists用法。分享给大家供大家参考。具体如下: 如果我们要判断一个类是不是可以用,可以先使用class_exists函数来判断一下,...

字符串长度函数strlen和mb_strlen的区别示例介绍

在php中常见的计算字符串长度的函数有:strlen和mb_strlen.当字符全是英文字符的时候,两者是一样。这里主要比较一下,中英文混排的时候,两个计算结果。 在PHP中,strle...

将CMYK颜色值和RGB颜色相互转换的PHP代码

function hex2rgb($hex) { $color = str_replace('#','',$hex); $rgb = array('r' => hexdec(s...

浅析十款PHP开发框架的对比

PHP开发框架近来在PHP社区中成为讨论的热点,几乎每天都在推出新的框架。面对市面上超过四十种的开发框架,你很难判断究竟哪一款最适合你,尤其是在这些框架所提供的功能不尽相同的时候。&nb...

PHP模拟asp中response类实现方法

本文实例讲述了PHP模拟asp中response类的方法。分享给大家供大家参考。具体如下: 习惯了asp或是asp.net开发的人, 他们会经常用到response类,这个类用于处理客户...