Php-implemented CSS update class instance

Source: Internet
Author: User

Php-implemented CSS update class instance

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

21

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

<? Php

/** Css update class: updates the image version in the css file.

* Date: 2013-02-05

* Author: fdipzone

* Ver: 1.1

*

* Func:

* Update ();

*

* Ver: 1.1 added the search_child parameter to traverse subfolders.

*/

Class CSSUpdate {

Private $ cs1_pl_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 image type to be replaced

* @ Param boolean $ search_child: whether to traverse subfolders. The default value is false.

*/

Public function _ construct ($ cs1_pl_path, $ css_path, $ replacetags = array (), $ search_child = false ){

If (! Is_dir ($ cs1_pl_path) |! Is_dir ($ css_path) |! $ Replacetags ){

$ This-> is_ready = 0;

} Else {

$ This-> cs1_pl_path = $ cs1_pl_path;

$ This-> css_path = $ css_path;

$ This-> replacetags = $ replacetags;

$ This-> search_child = $ search_child;

$ This-> is_ready = 1;

}

}

/** Update the css file */

Public function update (){

If ($ this-> is_ready = 0 ){

$ This-> response ('csstmpl or csspath or replacetags error ');

Return '';

}

$ This-> traversing ($ this-> cs1_pl_path );

$ This-> response ('covert num: '. $ this-> convert_num );

}

/** Traverse folders

* @ 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) {// You 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 the 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 the template content and write it to 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) {// generate the target path

Mkdir (dirname ($ dfile), 0755, true );

}

File_put_contents ($ dfile, $ css_content, true );

}

/** Output */

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

$ Cs1_pl_path = ROOT_PATH. '/csstmpl ';

$ Replacetags = array('.png ', '.jpg', '.gif ');

$ Cssobj = new CSSUpdate ($ cs1_pl_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.