Php regular expression replacement method for processing HTML pages _ PHP Tutorial

Source: Internet
Author: User
Php regular expression replacement method for processing HTML pages. Php regular expression replacement for processing HTML pages this article describes how php regular expression replacement for processing HTML pages. Share it with you for your reference. The details are as follows :? 1234567891011 php regular expression replacement method for processing HTML pages

This example describes how to use php regular expression replacement to process HTML pages. Share it with you for your reference. The details are 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

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

If (! Defined ('basepath') exit ('no direct script access allowed ');

/**

* HTML replacement processing class. Consider the following replacement methods:

* 1. img src: '/] + ?) /I'

* 2. a href: '/] + ?) /I'

* 3. ifram. src :'/ ] + ?) /I'

* 4. frame src :'/ ] + ?) /I'

* 5. js: '/window. open ([(] + ?) ([\ '"] + ?) (. + ?) ([) +?]) /I'

* 6. css: '/background (. + ?) Url ([(]) ([\ '"] + ?) (. + ?) ([) +?]) /I'

*/

Class Myreplace {

Private $ moudle_array = array ('udata', 'tdata', 'tresresult', 'dresresult ');

Private $ content;

Private $ relative_dirname;

Private $ projectid;

Private $ moudle;

Function _ construct (){

$ This-> CI = & get_instance ();

}

/**

* Replace

* @ Param string $ content HTML content

* @ Param string $ relative path of relative

* @ Param int $ projectid Project id

* @ Moudle string $ moudle template id: udata, tdata, tresult, dresult

*/

Public function my_replace ($ content, $ relative, $ projectid, $ moudle ){

$ This-> content = $ content;

$ This-> relative_dirname = $ relative;

$ This-> projectid = $ projectid;

If (in_array (strtolower ($ moudle), $ this-> moudle_array ))

$ This-> moudle = $ moudle;

Else exit;

Switch ($ this-> moudle ){

Case 'udata ':

$ This-> CI-> load-> model ('mupload _ data', 'model ');

Break;

Case 'tdata ':

$ This-> CI-> load-> model ('taskdata', 'model ');

Break;

Case 'result ':

$ This-> CI-> load-> model ('taskresult', 'model ');

Break;

Case 'dresresult ':

$ This-> CI-> load-> model ('dmsresult', 'model ');

Break;

Default:

Break;

}

$ Pattern = '/] + ?) /I ';

$ Content = preg_replace_callback ($ pattern, array ($ this, 'image _ replace '), $ content );

$ Pattern = '/] + ?) /I ';

$ Content = preg_replace_callback ($ pattern, array ($ this, 'HTML _ replace '), $ content );

$ Pattern = '/ ] + ?) /I ';

$ Content = preg_replace_callback ($ pattern, array ($ this, 'iframe _ replace '), $ content );

$ Pattern = '/ ] + ?) /I ';

$ Content = preg_replace_callback ($ pattern, array ($ this, 'frame _ replace '), $ content );

$ Pattern = '/window. open ([(] + ?) ([\ '"] + ?) (. + ?) ([)] + ?) /I ';

$ Content = preg_replace_callback ($ pattern, array ($ this, 'JS _ replace '), $ content );

$ Pattern = '/background (. + ?) Url ([(]) ([\ '"] + ?) (. + ?) ([) +?]) /I ';

$ Content = preg_replace_callback ($ pattern, array ($ this, 'css _ replace '), $ content );

Return $ content;

}

Private function image_replace ($ matches ){

If (count ($ matches) <4) return '';

If (empty ($ matches [3]) return '';

$ Matches [3] = rtrim ($ matches [3], '\' "/');

// Obtain the image id

$ Parent_dir_num = substr_count ($ matches [3], '.../');

$ Relative_dirname = $ this-> relative_dirname;

For ($ I = 0; $ I <$ parent_dir_num; $ I ++ ){

$ Relative_dirname = substr ($ relative_dirname, 0, strrpos ($ relative_dirname ,"/"));

}

$ Relativepath = rtrim ($ relative_dirname, '/'). '/'. ltrim ($ matches [3], './');

$ Image_id = $ this-> CI-> model-> get_id_by_path_and_project ($ relativepath, $ this-> projectid );

// Output

If (! Empty ($ image_id )){

If ($ this-> moudle = 'dresresult '){

Return "CI-> config-> item (" base_url ")." cdms/". $ this-> moudle."/readpic/$ image_id? Pid = ". $ this-> projectid. $ matches [2]. $ matches [4];

} Else {

Return "CI-> config-> item (" base_url ")." cdms/". $ this-> moudle."/picfile/$ image_id? Pid = ". $ this-> projectid. $ matches [2]. $ matches [4];

}

} Else {

Return"

}

}

Private function html_replace ($ matches ){

If (count ($ matches) <4) return '';

If (empty ($ matches [3]) return '';

// If the href link ($ matches [3]) starts with http, www, or mailto, no processing is performed.

// If (preg_match ('/^ [http | www | mailto] (. + ?) /I ', $ matches [3])

// Return"

$ Matches [3] = rtrim ($ matches [3], '\' "/');

// Process the anchor

If (substr_count ($ matches [3], '#')> 0)

$ Matches [3] = substr ($ matches [3], 0, strrpos ($ matches [3], '#');

// Obtain the html id

$ Parent_dir_num = substr_count ($ matches [3], '.../');

$ Relative_dirname = $ this-> relative_dirname;

For ($ I = 0; $ I <$ parent_dir_num; $ I ++ ){

$ Relative_dirname = substr ($ relative_dirname, 0, strrpos ($ relative_dirname ,"/"));

}

$ Relativepath = rtrim ($ relative_dirname, '/'). '/'. ltrim ($ matches [3], './');

$ Txtfile_id = $ this-> CI-> model-> get_id_by_path_and_project ($ relativepath, $ this-> projectid );

// Output

If (! Empty ($ txtfile_id )){

If ($ this-> moudle = 'dresresult '){

Return "CI-> config-> item (" base_url ")." cdms/". $ this-> moudle."/readfile/$ txtfile_id? Pid = ". $ this-> projectid. $ matches [2]. $ matches [4];

} Else {

Return "CI-> config-> item (" base_url ")." cdms/". $ this-> moudle."/txtfile/$ txtfile_id? Pid = ". $ this-> projectid. $ matches [2]. $ matches [4];

}

} Else {

Return"

}

}

Private function iframe_replace ($ matches ){

If (count ($ matches) <4) return '';

If (empty ($ matches [3]) return '';

$ Matches [3] = rtrim ($ matches [3], '\' "/');

// Process the anchor

If (substr_count ($ matches [3], '#')> 0)

$ Matches [3] = substr ($ matches [3], 0, strrpos ($ matches [3], '#');

// Obtain the html id

$ Parent_dir_num = substr_count ($ matches [3], '.../');

$ Relative_dirname = $ this-> relative_dirname;

For ($ I = 0; $ I <$ parent_dir_num; $ I ++ ){

$ Relative_dirname = substr ($ relative_dirname, 0, strrpos ($ relative_dirname ,"/"));

}

$ Relativepath = rtrim ($ relative_dirname, '/'). '/'. ltrim ($ matches [3], './');

$ Txtfile_id = $ this-> CI-> model-> get_id_by_path_and_project ($ relativepath, $ this-> projectid );

// Output

If (! Empty ($ txtfile_id )){

If ($ this-> moudle = 'dresresult '){

Return" CI-> config-> item ("base_url"). "cdms/". $ this-> moudle. "/readfile/$ txtfile_id? Pid = ". $ this-> projectid. $ matches [2]. $ matches [4];

} Else {

Return" CI-> config-> item ("base_url"). "cdms/". $ this-> moudle. "/txtfile/$ txtfile_id? Pid = ". $ this-> projectid. $ matches [2]. $ matches [4];

}

} Else {

Return"

}

}

Private function frame_replace ($ matches ){

If (count ($ matches) <4) return '';

If (empty ($ matches [3]) return '';

$ Matches [3] = rtrim ($ matches [3], '\' "/');

// Process the anchor

If (substr_count ($ matches [3], '#')> 0)

$ Matches [3] = substr ($ matches [3], 0, strrpos ($ matches [3], '#');

// Obtain the html id

$ Parent_dir_num = substr_count ($ matches [3], '.../');

$ Relative_dirname = $ this-> relative_dirname;

For ($ I = 0; $ I <$ parent_dir_num; $ I ++ ){

$ Relative_dirname = substr ($ relative_dirname, 0, strrpos ($ relative_dirname ,"/"));

}

$ Relativepath = rtrim ($ relative_dirname, '/'). '/'. ltrim ($ matches [3], './');

$ Txtfile_id = $ this-> CI-> model-> get_id_by_path_and_project ($ relativepath, $ this-> projectid );

// Output

If (! Empty ($ txtfile_id )){

If ($ this-> moudle = 'dresresult '){

Return" CI-> config-> item ("base_url"). "cdms/". $ this-> moudle. "/readfile/$ txtfile_id? Pid = ". $ this-> projectid. $ matches [2]. $ matches [4];

} Else {

Return" CI-> config-> item ("base_url"). "cdms/". $ this-> moudle. "/txtfile/$ txtfile_id? Pid = ". $ this-> projectid. $ matches [2]. $ matches [4];

}

} Else {

Return"

}

}

Private function js_replace ($ matches ){

If (count ($ matches) <4) return '';

If (empty ($ matches [3]) return '';

// Process the link

$ Arr_html = split (',', $ matches [3]);

$ Href = $ arr_html [0];

$ Other = '';

For ($ I = 0; $ I

$ Other = $ arr_html [$ I]. ",";

$ Other = rtrim ($ other ,"\,");

$ Href = rtrim ($ href ,'\'\"');

// Process the anchor

If (substr_count ($ href, '#')> 0)

Return "window. open". $ matches [1]. $ matches [2]. $ matches [3]. $ matches [4];

// Obtain the html id

$ Parent_dir_num = substr_count ($ href ,'../');

$ Relative_dirname = $ this-> relative_dirname;

For ($ I = 0; $ I <$ parent_dir_num; $ I ++ ){

$ Relative_dirname = substr ($ relative_dirname, 0, strrpos ($ relative_dirname ,"/"));

}

$ Relativepath = rtrim ($ relative_dirname, '/'). '/'. ltrim ($ href ,'./');

$ Txtfile_id = $ this-> CI-> model-> get_id_by_path_and_project ($ relativepath, $ this-> projectid );

// Output

If (! Empty ($ txtfile_id )){

If ($ this-> moudle = 'dresresult '){

Return "window. open ". $ matches [1]. $ matches [2]. $ this-> CI-> config-> item ("base_url "). "cdms /". $ this-> moudle. "/readfile/$ txtfile_id? Pid = ". $ this-> projectid. $ matches [2]. ','. $ other. $ matches [4];

} Else {

Return "window. open ". $ matches [1]. $ matches [2]. $ this-> CI-> config-> item ("base_url "). "cdms /". $ this-> moudle. "/txtfile/$ txtfile_id? Pid = ". $ this-> projectid. $ matches [2]. ','. $ other. $ matches [4];

}

} Else {

Return "window. open". $ matches [1]. $ matches [2]. $ matches [3]. $ matches [4];

}

}

Private function css_replace ($ matches ){

If (count ($ matches) <5) return '';

If (empty ($ matches [4]) return '';

$ Matches [4] = rtrim ($ matches [4], '\' "/');

// Obtain the image id

$ Parent_dir_num = substr_count ($ matches [4], '.../');

$ Relative_dirname = $ this-> relative_dirname;

For ($ I = 0; $ I <$ parent_dir_num; $ I ++ ){

$ Relative_dirname = substr ($ relative_dirname, 0, strrpos ($ relative_dirname ,"/"));

}

$ Relativepath = rtrim ($ relative_dirname, '/'). '/'. ltrim ($ matches [4], './');

$ Image_id = $ this-> CI-> model-> get_id_by_path_and_project ($ relativepath, $ this-> projectid );

// Output

If (! Empty ($ image_id )){

If ($ this-> moudle = 'dresresult '){

Return "background ". $ matches [1]. "url ". $ matches [2]. $ matches [3]. $ this-> CI-> config-> item ("base_url "). "cdms /". $ this-> moudle. "/readpic/$ image_id? Pid = ". $ this-> projectid. $ matches [3]. $ matches [5];

} Else {

Return "background ". $ matches [1]. "url ". $ matches [2]. $ matches [3]. $ this-> CI-> config-> item ("base_url "). "cdms /". $ this-> moudle. "/picfile/$ image_id? Pid = ". $ this-> projectid. $ matches [3]. $ matches [5];

}

} Else {

Return "background ". $ matches [1]. "url ". $ matches [2]. $ matches [3]. $ matches [4]. $ matches [3]. $ matches [5];

}

}

}

/* End of Myreplace. php */

/* Location:/application/libraries/Myreplace. php */

I hope this article will help you with php programming.

Examples in this article describes how to use php regular expression replacement to process HTML pages. Share it with you for your reference. The details are as follows :? 1 2 3 4 5 6 7 8 9 10 11...

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.