CSS update class implemented by PHP

Source: Internet
Author: User
  1. /** CSS Update class, update the version of the image within the CSS file
  2. * date:2013-02-05
  3. * Author:fdipzone
  4. * ver:1.1
  5. * edit:bbs.it-home.org
  6. * Func:
  7. * Update ();
  8. *
  9. * ver:1.1 Add search_child parameter, can traverse sub-folder
  10. */
  11. Class cssupdate{
  12. Private $csstmpl _path = null;
  13. Private $CSS _path = null;
  14. Private $replacetags = Array ();
  15. Private $search _child = false;
  16. Private $convert _num = 0;
  17. Private $is _ready = 0;
  18. /** initialization
  19. * @param String $csstmpl _path css template path
  20. * @param String $css _path CSS Target path
  21. * @param Array $replacetags The type of picture you want to replace
  22. * @param boolean $search _child whether to traverse subfolders, false by default
  23. */
  24. Public function __construct ($csstmpl _path, $css _path, $replacetags =array (), $search _child=false) {
  25. if (!is_dir ($csstmpl _path) | | |!is_dir ($css _path) | |! $replacetags) {
  26. $this->is_ready = 0;
  27. }else{
  28. $this->csstmpl_path = $csstmpl _path;
  29. $this->css_path = $css _path;
  30. $this->replacetags = $replacetags;
  31. $this->search_child = $search _child;
  32. $this->is_ready = 1;
  33. }
  34. }
  35. /** Updating CSS files */
  36. Public Function Update () {
  37. if ($this->is_ready==0) {
  38. $this->response (' Csstmpl or Csspath or replacetags error ');
  39. Return ';
  40. }
  41. $this->traversing ($this->csstmpl_path);
  42. $this->response (' Covert num: '. $this->convert_num);
  43. }
  44. /** traversing a folder
  45. * @param String $path file path
  46. */
  47. Private function traversing ($path) {
  48. $handle = Opendir ($path);
  49. while (($file =readdir ($handle))!==false) {
  50. if ($file! = ': ' && $file! = '. ') {
  51. $curfile = $path. ' /'. $file;
  52. if (Is_dir ($curfile)) {//Folder
  53. if ($this->search_child) {//need to traverse subfolders
  54. $this->traversing ($curfile);
  55. }
  56. }elseif ($this->checkext ($curfile)) {//CSS file
  57. $dfile = Str_replace ($this->csstmpl_path, $this->css_path, $curfile);
  58. $this->create ($curfile, $dfile);
  59. $this->response ($curfile. ' Convert to '. $dfile. ' Success ');
  60. $this->convert_num + +;
  61. }
  62. }
  63. }
  64. Closedir ($handle);
  65. }
  66. /** Check file suffix */
  67. Private Function Checkext ($file) {
  68. $name = basename ($file);
  69. $namefrag = Explode ('. ', $name);
  70. if (count ($namefrag) >=2) {
  71. if (Strtolower ($namefrag [Count ($namefrag)-1] = = = ' css ') {//CSS file
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. /** Replace template content, write Csspath
  78. * @param String $tmplfile template file
  79. * @param String $dfile target file
  80. */
  81. Private function Create ($tmplfile, $dfile) {
  82. $css _content = file_get_contents ($tmplfile);
  83. foreach ($this->replacetags as $tag) {
  84. $css _content = Str_replace ($tag, $tag. "?". Date (' Ymdhis '), $css _content);
  85. }
  86. if (!is_dir (DirName ($dfile))) {//Build target path
  87. mkdir (DirName ($dfile), 0755, true);
  88. }
  89. File_put_contents ($dfile, $css _content, True);
  90. }
  91. /** Output */
  92. Private function Response ($content) {
  93. echo $content. "
    ";
  94. }
  95. }
  96. ?>
Copy Code

2, demo example demo.php

    1. Require_once "CSSUpdate.class.php";
    2. Define (' Root_path ', DirName (__file__));
    3. $css _path = Root_path. ' /css ';
    4. $csstmpl _path = Root_path. ' /csstmpl ';
    5. $replacetags = Array ('. png ', '. jpg ', '. gif ');
    6. $cssobj = new Cssupdate ($csstmpl _path, $css _path, $replacetags);
    7. $cssobj->update ();
    8. ?>
Copy Code

Attached, PHP CSS Update class source download address.

  • 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.