PHP Implementation of CSS Update class instance

Source: Internet
Author: User

The CSSUpdate.class.php class files are as follows:

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 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 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105-106 <?php /** CSS Update class, update the version of the picture in the CSS file   *  date:  2013-02-05  *  author:fdipzone  *&nbs P ver:  1.1  *  *  func:  *  update ();  *  *  1.1 add ver:  parameters, Can traverse subfolders   */      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 need to replace the picture type    & nbsp;* @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) { //needs to traverse 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 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;    }        /** replaces the template content, writes csspath    * @param String $tmplfile Template file     * @param String $dfile   target file     */    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);      }  &Nbsp;   if (!is_dir (dirname ($dfile)) { //Build target path          mkdir (DirName ($dfile), 0755, True);      }      file_put_ Contents ($dfile, $css _content, True);    }        /** output */   & Nbsp;private function Response ($content) {      echo $content. " <br> ";    } } ?>

The Demo sample program is as follows:

1 2 3 4 5 6 7 8 9 10 11 <?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 (); ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.