@@ -23,7 +23,7 @@ class HtmlHelper
2323 */
2424 public static function encode ($ text , $ charset = 'utf-8 ' ): string
2525 {
26- return htmlspecialchars ($ text , ENT_QUOTES , $ charset );
26+ return \ htmlspecialchars ($ text , ENT_QUOTES , $ charset );
2727 }
2828
2929 /**
@@ -34,7 +34,7 @@ public static function encode($text, $charset = 'utf-8'): string
3434 */
3535 public static function decode ($ text ): string
3636 {
37- return htmlspecialchars_decode ($ text , ENT_QUOTES );
37+ return \ htmlspecialchars_decode ($ text , ENT_QUOTES );
3838 }
3939
4040 /**
@@ -50,11 +50,11 @@ public static function encodeArray($data, $charset = 'utf-8'): array
5050
5151 foreach ($ data as $ key => $ value ) {
5252 if (\is_string ($ key )) {
53- $ key = htmlspecialchars ($ key , ENT_QUOTES , $ charset );
53+ $ key = \ htmlspecialchars ($ key , ENT_QUOTES , $ charset );
5454 }
5555
5656 if (\is_string ($ value )) {
57- $ value = htmlspecialchars ($ value , ENT_QUOTES , $ charset );
57+ $ value = \ htmlspecialchars ($ value , ENT_QUOTES , $ charset );
5858 } elseif (\is_array ($ value )) {
5959 $ value = static ::encodeArray ($ value );
6060 }
@@ -86,19 +86,19 @@ public static function escape($data, int $type = 0, $encoding = 'UTF-8')
8686 $ data [$ k ] = self ::escape ($ data , $ type , $ encoding );
8787 }
8888
89+ return $ data ;
90+ }
91+
92+ // 默认使用 htmlspecialchars()
93+ if (!$ type ) {
94+ $ data = \htmlspecialchars ($ data , \ENT_QUOTES , $ encoding );
8995 } 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+ }
9698
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 );
102102 }
103103
104104 return $ data ;
@@ -119,9 +119,9 @@ public static function unescap($data, $type = 0, $encoding = 'UTF-8')
119119 }
120120
121121 } elseif (!$ type ) {//默认使用 htmlspecialchars_decode()
122- $ data = htmlspecialchars_decode ($ data , ENT_QUOTES );
122+ $ data = \ htmlspecialchars_decode ($ data , \ ENT_QUOTES );
123123 } else {
124- $ data = html_entity_decode ($ data , ENT_QUOTES , $ encoding );
124+ $ data = \ html_entity_decode ($ data , \ ENT_QUOTES , $ encoding );
125125 }
126126
127127 return $ data ;
@@ -144,7 +144,7 @@ public static function stripImages(string $string): string
144144 */
145145 public static function stripIframes (string $ string ): string
146146 {
147- return preg_replace ('#(<[/]?iframe.*>)#U ' , '' , $ string );
147+ return \ preg_replace ('#(<[/]?iframe.*>)#U ' , '' , $ string );
148148 }
149149
150150 /**
@@ -154,7 +154,7 @@ public static function stripIframes(string $string): string
154154 */
155155 public static function stripScript (string $ string ): string
156156 {
157- return preg_replace ('/<script[^>]*>.*?</script>/si ' , '' , $ string );
157+ return \ preg_replace ('/<script[^>]*>.*?</script>/si ' , '' , $ string );
158158 }
159159
160160 /**
@@ -164,25 +164,25 @@ public static function stripScript(string $string): string
164164 */
165165 public static function stripStyle (string $ string ): string
166166 {
167- return preg_replace ('/<style[^>]*>.*?</style>/si ' , '' , $ string );
167+ return \ preg_replace ('/<style[^>]*>.*?</style>/si ' , '' , $ string );
168168 }
169169
170170 /**
171171 * @param string $html
172172 * @param bool|true $onlySrc
173173 * @return array
174174 */
175- public static function findImages (string $ html , bool $ onlySrc = true ): array
175+ public static function matchImages (string $ html , bool $ onlySrc = true ): array
176176 {
177177 // $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*>/i';
178178 $ preg = '/<img.+src=\"(:?.+.+\.(?:jpg|gif|bmp|bnp|png)\"?).+>/i ' ;
179179
180- if (!preg_match_all ($ preg , trim ($ html ), $ images )) {
180+ if (!\ preg_match_all ($ preg , trim ($ html ), $ images )) {
181181 return [];
182182 }
183183
184184 if ($ onlySrc ) {
185- return array_key_exists (1 , $ images ) ? $ images [1 ] : [];
185+ return \ array_key_exists (1 , $ images ) ? $ images [1 ] : [];
186186 }
187187
188188 return $ images ;
@@ -194,7 +194,7 @@ public static function findImages(string $html, bool $onlySrc = true): array
194194 */
195195 public static function minify (string $ html ): string
196196 {
197- $ search = [
197+ $ search = [
198198 '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:| \\\| \')\/\/.*))/ ' ,
199199 '/\n/ ' ,
200200 '/\>[^\S ]+/s ' ,
@@ -203,6 +203,6 @@ public static function minify(string $html): string
203203 ];
204204 $ replace = [' ' , ' ' , '> ' , '< ' , '\\1 ' ];
205205
206- return preg_replace ($ search , $ replace , $ html );
206+ return \ preg_replace ($ search , $ replace , $ html );
207207 }
208208}
0 commit comments