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