Css Update class implemented by php

Source: Internet
Author: User
Css Update class implemented by php

  1. /** Css Update class: updates the image version in 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 added the search_child parameter to traverse subfolders.
  10. */
  11. Class CSSUpdate {
  12. Private $ cs1_pl_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 image type to be replaced
  22. * @ Param boolean $ search_child: whether to traverse subfolders. the default value is false.
  23. */
  24. Public function _ construct ($ cs1_pl_path, $ css_path, $ replacetags = array (), $ search_child = false ){
  25. If (! Is_dir ($ cs1_pl_path) |! Is_dir ($ css_path) |! $ Replacetags ){
  26. $ This-> is_ready = 0;
  27. } Else {
  28. $ This-> cs1_pl_path = $ cs1_pl_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. /** UPDATE The css file */
  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-> cs1_pl_path );
  42. $ This-> response ('covert num: '. $ this-> convert_num );
  43. }
  44. /** Traverse folders
  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) {// you 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 the 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 the template content and write it to 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) {// Generate the 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. ?>

2. demo. php

  1. Require_once "CSSUpdate. class. php ";
  2. Define ('root _ path', dirname (_ FILE __));
  3. $ Css_path = ROOT_PATH. '/css ';
  4. $ Cs1_pl_path = ROOT_PATH. '/csstmpl ';
  5. $ Replacetags = array('.png ', '.jpg', '.gif ');
  6. $ Cssobj = new CSSUpdate ($ cs1_pl_path, $ css_path, $ replacetags );
  7. $ Cssobj-> update ();
  8. ?>

Appendix: Source code of the php css Update class.

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.