php实现的CSS更新类实例
作者:shichen2014 时间:2023-11-21 22:28:30
本文实例讲述了php实现的CSS更新类及其用法,非常实用。分享给大家供大家参考。具体如下:
CSSUpdate.class.php类文件如下:
<?php
/** css 更新类,更新css文件内图片的版本
* Date: 2013-02-05
* Author: fdipzone
* Ver: 1.1
*
* Func:
* update();
*
* Ver: 1.1 增加search_child参数,可遍历子文件夹
*/
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目标路径
* @param Array $replacetags 需要替换的图片类型
* @param boolean $search_child 是否遍历子文件夹,默认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;
}
/** 替换模版内容,写入csspath
* @param String $tmplfile 模版文件
* @param String $dfile 目标文件
*/
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))){ // 生成目标路径
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();
?>
完整源码点击此处本站下载。
希望本文所述对大家PHP程序设计的学习有所帮助。
标签:php,CSS
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
讲解SQL Server海量数据导入的最快方法
2008-12-05 16:21:00
对python的unittest架构公共参数token提取方法详解
2023-10-18 12:28:54
asp利用dictionary创建二维数组
2009-02-02 09:54:00
jquery中获取id值方法小结
2024-04-19 10:19:25
MHTML在ie7/vista bug 解决方案
2010-02-01 12:42:00
Pandas多列值合并成一列的实现
2023-02-13 08:39:44
![](https://img.aspxhome.com/file/2023/5/107195_0s.png)
python游戏库pygame经典教程(推荐!)
2022-10-02 06:26:11
![](https://img.aspxhome.com/file/2023/8/110028_0s.png)
ElementUI日期选择器时间选择范围限制的实现
2024-04-09 11:00:28
python基于scrapy爬取京东笔记本电脑数据并进行简单处理和分析
2023-08-05 03:18:06
![](https://img.aspxhome.com/file/2023/2/72252_0s.png)
对Python3 pyc 文件的使用详解
2023-05-02 01:54:57
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
2021-11-22 08:11:12
pytorch中F.avg_pool1d()和F.avg_pool2d()的使用操作
2023-11-30 01:20:41
![](https://img.aspxhome.com/file/2023/3/87823_0s.png)
OpenCV基于ORB算法实现角点检测
2021-08-20 03:08:13
![](https://img.aspxhome.com/file/2023/6/127806_0s.jpg)
在HTML中,常见的URL有多种表示方式:
2009-07-28 12:18:00
python类的继承链实例分析
2023-05-16 14:04:36
ASP如何使用CDONTS来发送电子邮件?
2010-06-05 12:35:00
OverFlow:一个秘密武器
2011-02-26 15:41:00
Vue.js添加组件操作示例
2024-05-11 09:16:32
![](https://img.aspxhome.com/file/2023/7/126417_0s.png)
浅谈vue单一组件下动态修改数据时的全部重渲染
2024-04-27 15:51:55
深入浅析SQL Server 触发器
2024-01-19 05:23:55