This article illustrates the CSS update class and its usage in PHP, which is very practical. Share to everyone for your reference. Specifically as follows:
The CSSUpdate.class.php class files are as follows:
<?php/** CSS Update class, update the version of the picture in the CSS file * date:2013-02-05 * author:fdipzone * ver:1.1 * Func: * update ();
* * ver:1.1 Add search_child parameters, can traverse subfolder/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; /** initialization * @param string $csstmpl _path css Template path * @param String $css _path CSS Target path * @param Array $replacetags Required Replaced picture type * @param boolean $search _child whether to traverse subfolders, default 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;
} /** Update CSS File */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);
/** Traverse Folder * @param String $path File 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) {//need to traverse subfolders $this->traversing ($CU
Rfile); }}elseif ($this->checkext ($curfile)) {//css file $dfile = str_replace ($this->csstmpl_path, $thi
S->css_path, $curfile);
$this->create ($curfile, $dfile);
$this->response ($curfile. ' Convert to '. $dfile. ' Success '); $this->cOnvert_num + +;
}} closedir ($handle);
/** Check file suffix * * Private function Checkext ($file) {$name = basename ($file);
$namefrag = Explode ('. ', $name);
if (count ($namefrag) >=2) {if (Strtolower ($namefrag [Count ($namefrag)-1]) = = ' css ') {//CSS file return true;
return false; /** Replace template content, write Csspath * @param string $tmplfile template file * @param string $dfile target file */Private function CR
Eate ($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)) {//Generate target path mkdir (dirname ($dfile), 0755, true);
} file_put_contents ($dfile, $css _content, True); /** Output */Private Function response ($content) {echo $content. "
<br> ";
}}?>
The Demo sample program is as follows:
<?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 ();
? >
Full source Click here to download the site.
I hope this article will help you with the learning of PHP programming.