PHP Implementation of CSS Update class instance _php tutorial

Source: Internet
Author: User

CSS update class instance implemented by PHP


The CSSUpdate.class.php class file is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

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

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

/** CSS Update class, update the version of the image within the CSS file

* date:2013-02-05

* Author:fdipzone

* ver:1.1

*

* Func:

* Update ();

*

* ver:1.1 Add search_child parameter, can traverse sub-folder

*/

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 The type of picture you want to replace

* @param boolean $search _child whether to traverse subfolders, false by default

*/

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;

}

}

/** Updating CSS files */

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);

}

/** traversing a 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) {//need 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;

}

/** Replace 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);

foreach ($this->replacetags as $tag) {

$css _content = Str_replace ($tag, $tag. "?". Date (' Ymdhis '), $css _content);

}

if (!is_dir (DirName ($dfile))) {//Build 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:

1

2

3

4

5

6

7

8

9

10

11

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 ();

?>

http://www.bkjia.com/PHPjc/882705.html www.bkjia.com true http://www.bkjia.com/PHPjc/882705.html techarticle PHP Implementation of the CSS update class instance CSSUpdate.class.php class file 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 the above-the--...

  • Related Article

    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.