php實(shí)現(xiàn)的CSS更新類實(shí)例
本文實(shí)例講述了php實(shí)現(xiàn)的CSS更新類及其用法,非常實(shí)用。分享給大家供大家參考。具體如下:
CSSUpdate.class.php類文件如下:
<?php
/** css 更新類,更新css文件內(nèi)圖片的版本
* Date: 2013-02-05
* Author: fdipzone
* Ver: 1.1
*
* Func:
* update();
*
* Ver: 1.1 增加search_child參數(shù),可遍歷子文件夾
*/
class CSSUpdate{
private $csstmpl_path = null;
private $css_path = null;
private $replacetags = array();
private $search_child = false;
private $convert_num = 0;
private $is_ready = 0;
/** 初始化
* @param String $csstmpl_path css模版路徑
* @param String $css_path css目標(biāo)路徑
* @param Array $replacetags 需要替換的圖片類型
* @param boolean $search_child 是否遍歷子文件夾,默認(rèn)false
*/
public function __construct($csstmpl_path, $css_path, $replacetags=array(), $search_child=false){
if(!is_dir($csstmpl_path) || !is_dir($css_path) || !$replacetags){
$this->is_ready = 0;
}else{
$this->csstmpl_path = $csstmpl_path;
$this->css_path = $css_path;
$this->replacetags = $replacetags;
$this->search_child = $search_child;
$this->is_ready = 1;
}
}
/** 更新css文件 */
public function update(){
if($this->is_ready==0){
$this->response('csstmpl or csspath or replacetags error');
return '';
}
$this->traversing($this->csstmpl_path);
$this->response('covert num:'.$this->convert_num);
}
/** 遍歷文件夾
* @param String $path 文件路徑
*/
private function traversing($path){
$handle = opendir($path);
while(($file=readdir($handle))!==false){
if($file!='..' && $file!='.'){
$curfile = $path.'/'.$file;
if(is_dir($curfile)){ // folder
if($this->search_child){ // 需要遍歷子文件夾
$this->traversing($curfile);
}
}elseif($this->checkExt($curfile)){ // css file
$dfile = str_replace($this->csstmpl_path, $this->css_path, $curfile);
$this->create($curfile, $dfile);
$this->response($curfile.' convert to '.$dfile.' success');
$this->convert_num ++;
}
}
}
closedir($handle);
}
/** 檢查文件后綴 */
private function checkExt($file){
$name = basename($file);
$namefrag = explode('.', $name);
if(count($namefrag)>=2){
if(strtolower($namefrag[count($namefrag)-1])=='css'){ // css文件
return true;
}
}
return false;
}
/** 替換模版內(nèi)容,寫(xiě)入csspath
* @param String $tmplfile 模版文件
* @param String $dfile 目標(biāo)文件
*/
private function create($tmplfile, $dfile){
$css_content = file_get_contents($tmplfile);
foreach($this->replacetags as $tag){
$css_content = str_replace($tag, $tag."?".date('YmdHis'), $css_content);
}
if(!is_dir(dirname($dfile))){ // 生成目標(biāo)路徑
mkdir(dirname($dfile), 0755, true);
}
file_put_contents($dfile, $css_content, true);
}
/** 輸出 */
private function response($content){
echo $content."<br>";
}
}
?>
demo示例程序如下:
<?php
require_once "CSSUpdate.class.php";
define('ROOT_PATH', dirname(__FILE__));
$css_path = ROOT_PATH.'/css';
$csstmpl_path = ROOT_PATH.'/csstmpl';
$replacetags = array('.png', '.jpg', '.gif');
$cssobj = new CSSUpdate($csstmpl_path, $css_path, $replacetags);
$cssobj->update();
?>
完整源碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家PHP程序設(shè)計(jì)的學(xué)習(xí)有所幫助。
相關(guān)文章
PHP使用curl函數(shù)發(fā)送Post請(qǐng)求的注意事項(xiàng)
這篇文章主要給大家介紹的是PHP使用curl函數(shù)發(fā)送Post請(qǐng)求的一些注意事項(xiàng),文中通過(guò)示例代碼與解釋介紹的很詳細(xì),對(duì)大家學(xué)習(xí)或則使用PHP具有一定的參考借鑒價(jià)值,有需要的朋友們可以跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
php實(shí)現(xiàn)遍歷多維數(shù)組的方法
這篇文章主要介紹了php實(shí)現(xiàn)遍歷多維數(shù)組的方法,涉及php針對(duì)多維數(shù)組的遍歷與遞歸操作實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
PHP實(shí)現(xiàn)的激活用戶注冊(cè)驗(yàn)證郵箱功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的激活用戶注冊(cè)驗(yàn)證郵箱功能,詳細(xì)分析了php郵件激活用戶所涉及的數(shù)據(jù)庫(kù)、郵件相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
php封裝的數(shù)據(jù)庫(kù)函數(shù)與用法示例【參考thinkPHP】
這篇文章主要介紹了php封裝的數(shù)據(jù)庫(kù)函數(shù)與用法,基于thinkPHP中數(shù)據(jù)庫(kù)操作相關(guān)代碼整理簡(jiǎn)化而來(lái),包括針對(duì)數(shù)據(jù)庫(kù)的設(shè)置、連接、查詢及日志操作等功能,簡(jiǎn)單實(shí)用,需要的朋友可以參考下2016-11-11
解決File size limit exceeded 錯(cuò)誤的方法
本篇文章是對(duì)File size limit exceeded 錯(cuò)誤進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
php自定義urlencode,urldecode函數(shù)實(shí)例
這篇文章主要介紹了php自定義urlencode,urldecode函數(shù),實(shí)例分析了php字符串轉(zhuǎn)碼的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
php識(shí)別翻轉(zhuǎn)iphone拍攝的顛倒圖片
這篇文章主要介紹了php識(shí)別翻轉(zhuǎn)iphone拍攝的顛倒圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

