用php實(shí)現(xiàn)的下載css文件中的圖片的代碼
更新時(shí)間:2010年02月08日 08:31:02 作者:
非常有創(chuàng)意的利用php獲取css中圖片地址并實(shí)現(xiàn)下載的代碼。
作為一個(gè)資深并且專業(yè)的扒皮人員,在我從初三開始投入偉大的互聯(lián)網(wǎng)中到現(xiàn)在積累了豐富的扒皮經(jīng)驗(yàn)。我相信每個(gè)做web的程序員也都會(huì)有類似的經(jīng)歷。
在扒皮過(guò)程中,必不可少的需要下載樣式文件中的圖片。碰到比較龐大的樣式文件,其中可能會(huì)有上百個(gè)需要下載的圖片,那么使用下面這段小代碼是最為合適的了。
< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.
Author: kimi
Documentation: 下載樣式文件中的圖片,水水專用扒皮工具
*/
//note 設(shè)置PHP超時(shí)時(shí)間
set_time_limit(0);
//note 取得樣式文件內(nèi)容
$styleFileContent = file_get_contents('images/style.css');
//note 匹配出需要下載的URL地址
preg_match_all("/url\((.*)\)/", $styleFileContent, $imagesURLArray);
//note 循環(huán)需要下載的地址,逐個(gè)下載
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}
以上是轉(zhuǎn)載的原文,下面是修改版本,轉(zhuǎn)載的話請(qǐng)留個(gè)鏈接。
<?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))
{
//創(chuàng)建目錄
createDir($dir);
}
$imagesURL='http://www.dbjr.com.cn/'.$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 ;
}
?>
在扒皮過(guò)程中,必不可少的需要下載樣式文件中的圖片。碰到比較龐大的樣式文件,其中可能會(huì)有上百個(gè)需要下載的圖片,那么使用下面這段小代碼是最為合適的了。
復(fù)制代碼 代碼如下:
< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.
Author: kimi
Documentation: 下載樣式文件中的圖片,水水專用扒皮工具
*/
//note 設(shè)置PHP超時(shí)時(shí)間
set_time_limit(0);
//note 取得樣式文件內(nèi)容
$styleFileContent = file_get_contents('images/style.css');
//note 匹配出需要下載的URL地址
preg_match_all("/url\((.*)\)/", $styleFileContent, $imagesURLArray);
//note 循環(huán)需要下載的地址,逐個(gè)下載
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}
以上是轉(zhuǎn)載的原文,下面是修改版本,轉(zhuǎn)載的話請(qǐng)留個(gè)鏈接。
復(fù)制代碼 代碼如下:
<?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))
{
//創(chuàng)建目錄
createDir($dir);
}
$imagesURL='http://www.dbjr.com.cn/'.$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正則匹配文章中的遠(yuǎn)程圖片地址并下載圖片至本地
- PHP使用pcntl_fork實(shí)現(xiàn)多進(jìn)程下載圖片的方法
- PHP通過(guò)正則表達(dá)式下載圖片到本地的實(shí)現(xiàn)代碼
- 使用PHP實(shí)現(xiàn)下載CSS文件中的圖片
- php使用gzip壓縮傳輸js和css文件的方法
- php獲取CSS文件中圖片地址并下載到本地的方法
- php實(shí)現(xiàn)的css文件背景圖片下載器代碼
- PHP根據(jù)傳入?yún)?shù)合并多個(gè)JS和CSS文件的簡(jiǎn)單實(shí)現(xiàn)
- 使用PHP下載CSS文件中的圖片的代碼
- 使用PHP下載CSS文件中的所有圖片【幾行代碼即可實(shí)現(xiàn)】
相關(guān)文章
使用PHP會(huì)話(Session)實(shí)現(xiàn)用戶登陸功能
本篇文章是對(duì)PHP會(huì)話(Session)實(shí)現(xiàn)用戶登陸功能進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP+MariaDB數(shù)據(jù)庫(kù)操作基本技巧備忘總結(jié)
這篇文章主要介紹了PHP+MariaDB數(shù)據(jù)庫(kù)操作基本技巧,結(jié)合實(shí)例形式總結(jié)分析了PHP+MariaDB數(shù)據(jù)庫(kù)連接、判斷以及基于PHP+MariaDB的用戶登陸、管理、刪除等相關(guān)操作實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05在Windows系統(tǒng)下使用PHP生成Word文檔的教程
這篇文章主要介紹了在Windows系統(tǒng)下使用PHP生成Word文檔的教程,要學(xué)習(xí)PHP的同學(xué)可以通過(guò)這樣的方式來(lái)練練手^^需要的朋友可以參考下2015-07-07php中使用PHPExcel讀寫excel(xls)文件的方法
這篇文章主要介紹了php中使用PHPExcel讀寫excel(xls)文件的方法,phpExcel是常用的用于操作Excel的PHP類庫(kù),應(yīng)用非常廣泛。需要的朋友可以參考下2014-09-09PHP中strnatcmp()函數(shù)“自然排序算法”進(jìn)行字符串比較用法分析(對(duì)比strcmp函數(shù))
這篇文章主要介紹了PHP中strnatcmp()函數(shù)“自然排序算法”進(jìn)行字符串比較用法,結(jié)合實(shí)例形式分析了strnatcmp函數(shù)字符串比較的使用技巧,并對(duì)比了strcmp函數(shù)說(shuō)明了兩者的使用區(qū)別,需要的朋友可以參考下2016-01-01