@@ -23,7 +23,7 @@ class HtmlHelper
23
23
*/
24
24
public static function encode ($ text , $ charset = 'utf-8 ' ): string
25
25
{
26
- return htmlspecialchars ($ text , ENT_QUOTES , $ charset );
26
+ return \ htmlspecialchars ($ text , ENT_QUOTES , $ charset );
27
27
}
28
28
29
29
/**
@@ -34,7 +34,7 @@ public static function encode($text, $charset = 'utf-8'): string
34
34
*/
35
35
public static function decode ($ text ): string
36
36
{
37
- return htmlspecialchars_decode ($ text , ENT_QUOTES );
37
+ return \ htmlspecialchars_decode ($ text , ENT_QUOTES );
38
38
}
39
39
40
40
/**
@@ -50,11 +50,11 @@ public static function encodeArray($data, $charset = 'utf-8'): array
50
50
51
51
foreach ($ data as $ key => $ value ) {
52
52
if (\is_string ($ key )) {
53
- $ key = htmlspecialchars ($ key , ENT_QUOTES , $ charset );
53
+ $ key = \ htmlspecialchars ($ key , ENT_QUOTES , $ charset );
54
54
}
55
55
56
56
if (\is_string ($ value )) {
57
- $ value = htmlspecialchars ($ value , ENT_QUOTES , $ charset );
57
+ $ value = \ htmlspecialchars ($ value , ENT_QUOTES , $ charset );
58
58
} elseif (\is_array ($ value )) {
59
59
$ value = static ::encodeArray ($ value );
60
60
}
@@ -86,19 +86,19 @@ public static function escape($data, int $type = 0, $encoding = 'UTF-8')
86
86
$ data [$ k ] = self ::escape ($ data , $ type , $ encoding );
87
87
}
88
88
89
+ return $ data ;
90
+ }
91
+
92
+ // 默认使用 htmlspecialchars()
93
+ if (!$ type ) {
94
+ $ data = \htmlspecialchars ($ data , \ENT_QUOTES , $ encoding );
89
95
} else {
90
- // 默认使用 htmlspecialchars()
91
- if (!$ type ) {
92
- $ data = htmlspecialchars ($ data , ENT_QUOTES , $ encoding );
93
- } else {
94
- $ data = htmlentities ($ data , ENT_QUOTES , $ encoding );
95
- }
96
+ $ data = \htmlentities ($ data , \ENT_QUOTES , $ encoding );
97
+ }
96
98
97
- //如‘志’这样的16进制的html字符,为了防止这样的字符被错误转译,使用正则进行匹配,把这样的字符又转换回来。
98
- if (strpos ($ data , '&# ' )) {
99
- $ data = preg_replace ('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/ ' ,
100
- '& \\1 ' , $ data );
101
- }
99
+ //如‘志’这样的16进制的html字符,为了防止这样的字符被错误转译,使用正则进行匹配,把这样的字符又转换回来。
100
+ if (\strpos ($ data , '&# ' )) {
101
+ $ data = \preg_replace ('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/ ' , '& \\1 ' , $ data );
102
102
}
103
103
104
104
return $ data ;
@@ -119,9 +119,9 @@ public static function unescap($data, $type = 0, $encoding = 'UTF-8')
119
119
}
120
120
121
121
} elseif (!$ type ) {//默认使用 htmlspecialchars_decode()
122
- $ data = htmlspecialchars_decode ($ data , ENT_QUOTES );
122
+ $ data = \ htmlspecialchars_decode ($ data , \ ENT_QUOTES );
123
123
} else {
124
- $ data = html_entity_decode ($ data , ENT_QUOTES , $ encoding );
124
+ $ data = \ html_entity_decode ($ data , \ ENT_QUOTES , $ encoding );
125
125
}
126
126
127
127
return $ data ;
@@ -144,7 +144,7 @@ public static function stripImages(string $string): string
144
144
*/
145
145
public static function stripIframes (string $ string ): string
146
146
{
147
- return preg_replace ('#(<[/]?iframe.*>)#U ' , '' , $ string );
147
+ return \ preg_replace ('#(<[/]?iframe.*>)#U ' , '' , $ string );
148
148
}
149
149
150
150
/**
@@ -154,7 +154,7 @@ public static function stripIframes(string $string): string
154
154
*/
155
155
public static function stripScript (string $ string ): string
156
156
{
157
- return preg_replace ('/<script[^>]*>.*?</script>/si ' , '' , $ string );
157
+ return \ preg_replace ('/<script[^>]*>.*?</script>/si ' , '' , $ string );
158
158
}
159
159
160
160
/**
@@ -164,25 +164,25 @@ public static function stripScript(string $string): string
164
164
*/
165
165
public static function stripStyle (string $ string ): string
166
166
{
167
- return preg_replace ('/<style[^>]*>.*?</style>/si ' , '' , $ string );
167
+ return \ preg_replace ('/<style[^>]*>.*?</style>/si ' , '' , $ string );
168
168
}
169
169
170
170
/**
171
171
* @param string $html
172
172
* @param bool|true $onlySrc
173
173
* @return array
174
174
*/
175
- public static function findImages (string $ html , bool $ onlySrc = true ): array
175
+ public static function matchImages (string $ html , bool $ onlySrc = true ): array
176
176
{
177
177
// $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*>/i';
178
178
$ preg = '/<img.+src=\"(:?.+.+\.(?:jpg|gif|bmp|bnp|png)\"?).+>/i ' ;
179
179
180
- if (!preg_match_all ($ preg , trim ($ html ), $ images )) {
180
+ if (!\ preg_match_all ($ preg , trim ($ html ), $ images )) {
181
181
return [];
182
182
}
183
183
184
184
if ($ onlySrc ) {
185
- return array_key_exists (1 , $ images ) ? $ images [1 ] : [];
185
+ return \ array_key_exists (1 , $ images ) ? $ images [1 ] : [];
186
186
}
187
187
188
188
return $ images ;
@@ -194,7 +194,7 @@ public static function findImages(string $html, bool $onlySrc = true): array
194
194
*/
195
195
public static function minify (string $ html ): string
196
196
{
197
- $ search = [
197
+ $ search = [
198
198
'/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:| \\\| \')\/\/.*))/ ' ,
199
199
'/\n/ ' ,
200
200
'/\>[^\S ]+/s ' ,
@@ -203,6 +203,6 @@ public static function minify(string $html): string
203
203
];
204
204
$ replace = ['' , '' , '> ' , '< ' , '\\1 ' ];
205
205
206
- return preg_replace ($ search , $ replace , $ html );
206
+ return \ preg_replace ($ search , $ replace , $ html );
207
207
}
208
208
}
0 commit comments