This article mainly introduces the CSS Update class implemented by php and its usage examples, including functions such as checking template files, updating and replacing template files, which are very practical, for more information, see the examples in this article to describe the CSS Update class implemented by php and its usage. Share it with you for your reference. The details are as follows:
The CSSUpdate. class. php class file is as follows:
<? Php/** css update class: update the image version in the css file * Date: 2013-02-05 * Author: fdipzone * Ver: 1.1 ** Func: * update (); ** Ver: 1.1 added the search_child parameter to traverse subfolders */class CSSUpdate {private $ cs1_pl_path = null; private $ css_path = null; private $ replacetags = array (); private $ search_child = false; private $ convert_num = 0; private $ is_ready = 0;/** initialize * @ param String $ cs1_pl_path css template path * @ param String $ css_path cs S target path * @ param Array $ replacetags indicates the image type to be replaced * @ param boolean $ whether search_child traverses subfolders. the default value is false */public function _ construct ($ cs1_pl_path, $ css_path, $ replacetags = array (), $ search_child = false) {if (! Is_dir ($ cs1_pl_path) |! Is_dir ($ css_path) |! $ Replacetags) {$ this-> is_ready = 0;} else {$ this-> cs1_pl_path = $ cs1_pl_path; $ this-> css_path = $ css_path; $ this-> replacetags = $ replacetags; $ this-> search_child = $ search_child; $ this-> is_ready = 1 ;}} /** update The css file */public function update () {if ($ this-> is_ready = 0) {$ this-> response ('csstmpl or csspath or replacetags error'); return '';} $ this-> traversing ($ this-> cs1_pl_path ); $ this-> response (' Covert num :'. $ this-> convert_num);}/** traverse the 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) {// you need to traverse the subfolders $ 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 '. $ dfile. 'Success'); $ this-> convert_num ++; }}} closedir ($ handle);}/** check Query the file suffix */private function checkExt ($ file) {$ name = basename ($ file); $ namefrag = explode ('. ', $ name); if (count ($ namefrag)> = 2) {if (strtolower ($ namefrag [count ($ namefrag)-1]) = 'css ') {// return true;} return false;}/** replace the template content, write csspath * @ param String $ tmplfile template file * @ param String $ dfile target file */private function create ($ tmplfile, $ dfile) {$ css_content = file_get_contents ($ tmplfile ); foreac H ($ this-> replacetags as $ tag) {$ css_content = str_replace ($ tag, $ tag ."? ". Date ('ymdhis '), $ css_content);} if (! Is_dir (dirname ($ dfile) {// Generate the target path mkdir (dirname ($ dfile), 0755, true);} file_put_contents ($ dfile, $ css_content, true);}/** output */private function response ($ content) {echo $ content."
";}}?>
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(); ?>
Click here to download the complete source code.
I hope this article will help you learn PHP programming.