摘要:本文主要向大家介绍了PHP语言的CSS Update Class,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
本文主要向大家介绍了PHP语言的CSS Update Class,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。
[php] view plain copy
1. <?php
2. /** css 更新类,更新css文件内图片的版本
3. * Date: 2013-02-05
4. * Author: fdipzone
5. * Ver: 1.1
6. *
7. * Func:
8. * update();
9. *
10. * Ver: 1.1 增加search_child参数,可遍历子文件夹
11. */
12.
13. class CSSUpdate{
14.
15. private $csstmpl_path = null;
16. private $css_path = null;
17. private $replacetags = array();
18. private $search_child = false;
19. private $convert_num = 0;
20. private $is_ready = 0;
21.
22. /** 初始化
23. * @param String $csstmpl_path css模版路径
24. * @param String $css_path css目标路径
25. * @param Array $replacetags 需要替换的图片类型
26. * @param boolean $search_child 是否遍历子文件夹,默认false
27. */
28. public function __construct($csstmpl_path, $css_path, $replacetags=array(), $search_child=false){
29. if(!is_dir($csstmpl_path) || !is_dir($css_path) || !$replacetags){
30. $this->is_ready = 0;
31. }else{
32. $this->csstmpl_path = $csstmpl_path;
33. $this->css_path = $css_path;
34. $this->replacetags = $replacetags;
35. $this->search_child = $search_child;
36. $this->is_ready = 1;
37. }
38. }
39.
40.
41. /** 更新css文件 */
42. public function update(){
43. if($this->is_ready==0){
44. $this->response('csstmpl or csspath or replacetags error');
45. return '';
46. }
47.
48. $this->traversing($this->csstmpl_path);
49.
50. $this->response('covert num:'.$this->convert_num);
51. }
52.
53.
54. /** 遍历文件夹
55. * @param String $path 文件路径
56. */
57. private function traversing($path){
58. $handle = opendir($path);
59. while(($file=readdir($handle))!==false){
60. if($file!='..' && $file!='.'){
61. $curfile = $path.'/'.$file;
62.
63. if(is_dir($curfile)){ // folder
64. if($this->search_child){ // 需要遍历子文件夹
65. $this->traversing($curfile);
66. }
67. }elseif($this->checkExt($curfile)){ // css file
68. $dfile = str_replace($this->csstmpl_path, $this->css_path, $curfile);
69. $this->create($curfile, $dfile);
70. $this->response($curfile.' convert to '.$dfile.' success');
71. $this->convert_num ++;
72. }
73.
74. }
75. }
76. closedir($handle);
77. }
78.
79.
80. /** 检查文件后缀 */
81. private function checkExt($file){
82. $name = basename($file);
83. $namefrag = explode('.', $name);
84. if(count($namefrag)>=2){
85. if(strtolower($namefrag[count($namefrag)-1])=='css'){ // css文件
86. return true;
87. }
88. }
89.
90. return false;
91. }
92.
93.
94. /** 替换模版内容,写入csspath
95. * @param String $tmplfile 模版文件
96. * @param String $dfile 目标文件
97. */
98. private function create($tmplfile, $dfile){
99. $css_content = file_get_contents($tmplfile);
100.
101. foreach($this->replacetags as $tag){
102. $css_content = str_replace($tag, $tag."?".date('YmdHis'), $css_content);
103. }
104.
105. if(!is_dir(dirname($dfile))){ // 生成目标路径
106. mkdir(dirname($dfile), 0755, true);
107. }
108.
109. file_put_contents($dfile, $css_content, true);
110. }
111.
112.
113. /** 输出 */
114. private function response($content){
115. echo $content."<br>";
116. }
117.
118. }
119.
120. ?>
demo:
[php] view plain copy
1. <?php
2.
3. require_once "CSSUpdate.class.php";
4.
5. define('ROOT_PATH', dirname(__FILE__));
6.
7. $css_path = ROOT_PATH.'/css';
8. $csstmpl_path = ROOT_PATH.'/csstmpl';
9. $replacetags = array('.png', '.jpg', '.gif');
10.
11. $cssobj = new CSSUpdate($csstmpl_path, $css_path, $replacetags);
12. $cssobj->update();
13.
14. ?>
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言PHP频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号