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);
    }  

    /** 遍历文件夹
	* 查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/
    * @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();  

?>

源码下载地址:http://download.csdn.net/detail/fdipzone/5786377

查看本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php
, http
, download
, 栏目
, www
源码下载
,以便于您获取更多的相关知识。

时间: 2024-08-31 08:06:35

php类:css更新类的相关文章

php实现的CSS更新类实例

 CSSUpdate.class.php类文件如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 7

php实现的CSS更新类实例_php技巧

本文实例讲述了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_

SqlCommandBuilder类批量更新excel或者CSV数据的方法_实用技巧

当你批量操作数据的时候,常常会使用到update table1 from table2的这种方式,这种方式是最简洁的. 但当你从excel或者CSV文件更新海量数据时,再使用这种方法,那性能是不是好?字符串拼接又何其之多,大数据是不是需要分组更新? 我不想去检测各种方法的速度,因为我个人比较认可以下方式,欢迎大家批评与指正. 我需要使用到的类主要是SqlCommandBuilder. /// <param name="table">准备更新的DataTable新数据<

css教程:CSS伪类

CSS pseudo-classes are used to add special effects to some selectors. CSS伪类可用来给一些选择器加上特殊效果. Hyperlink 如何在文档内给超级连接加上不同的颜色 This example demonstrates how to add different colors to a hyperlink in a document. Examples 实例:  代码如下 复制代码 Source Code to Run []

css并列类分类器的用法

问题描述 css并列类分类器的用法 css文件里面的并列类分类器是怎么应用的? 例如.A和.B并列(中间空格隔开),样式都是颜色为红色.为什么测试的时候文本的颜色还是黑色?这种情况怎么用? <html> <head> <style type="text/css"> .A .B {color: red;} </style> </head> <body> <p class="A">Tes

css 伪类的使用方法详解

css 伪类这个词很奇怪, 在我看来更像标识了特殊状态的标签. 和程序中的类什么的, 一点关系都没有. 伪类用:跟在选择器后面,标识了一些状态 ::表示 css3 新增的一些伪类, 建议全部都使用::.这年头还不支持 css3 的浏览器可以不管了 比如这几个伪类: link 链接还没被用户点击 visited 已经点击的链接 hover 鼠标指针悬停在链接上 active 链接正在被点击(鼠标在元素上按下,还没有释放) ::first-letter 给我感觉更像一个增强选择器 p::first-

CSS伪类:before, :after详解

 代码如下 复制代码 <!doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>伪类导个航..</title> <style type="text/css"> /*css部分, 留意其中border, marg

PHP遍历文件夹与文件类及处理类用法实例

  本文实例讲述了PHP遍历文件夹与文件类及处理类用法,非常具有实用价值.分享给大家供大家参考.具体方法如下: FindFile.class.php类文件用于遍历目录文件,具体代码如下: <?php /** 遍历文件夹及文件类 * Date: 2013-03-21 * Author: fdipzone * Ver: 1.0 */ class FindFile{ public $files = array(); // 存储遍历的文件 protected $maxdepth; // 搜寻深度,0表示

PHP遍历文件夹与文件类及处理类用法实例_php技巧

本文实例讲述了PHP遍历文件夹与文件类及处理类用法,非常具有实用价值.分享给大家供大家参考.具体方法如下: FindFile.class.php类文件用于遍历目录文件,具体代码如下: <?php /** 遍历文件夹及文件类 * Date: 2013-03-21 * Author: fdipzone * Ver: 1.0 */ class FindFile{ public $files = array(); // 存储遍历的文件 protected $maxdepth; // 搜寻深度,0表示没有