99
1010namespace Toolkit \Stdlib \Str ;
1111
12- use function is_string ;
13- use function htmlspecialchars ;
12+ use function array_key_exists ;
13+ use function html_entity_decode ;
1414use function htmlentities ;
15- use function strpos ;
16- use function is_array ;
15+ use function htmlspecialchars ;
1716use function htmlspecialchars_decode ;
18- use function html_entity_decode ;
17+ use function is_array ;
18+ use function is_string ;
1919use function preg_match_all ;
20- use function array_key_exists ;
20+ use function preg_replace ;
21+ use function strpos ;
2122
2223/**
2324 * Class HtmlHelper
25+ *
2426 * @package Toolkit\Stdlib\Str
2527 */
2628class HtmlHelper
2729{
2830 /**
2931 * Encodes special characters into HTML entities.
32+ *
3033 * @param string $text data to be encoded
3134 * @param string $charset
35+ *
3236 * @return string the encoded data
3337 * @see http://www.php.net/manual/en/function.htmlspecialchars.php
3438 */
35- public static function encode ($ text , $ charset = 'utf-8 ' ): string
39+ public static function encode (string $ text , string $ charset = 'utf-8 ' ): string
3640 {
3741 return htmlspecialchars ($ text , ENT_QUOTES , $ charset );
3842 }
3943
4044 /**
4145 * This is the opposite of {@link encode()}.
46+ *
4247 * @param string $text data to be decoded
48+ *
4349 * @return string the decoded data
4450 * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php
4551 */
46- public static function decode ($ text ): string
52+ public static function decode (string $ text ): string
4753 {
4854 return htmlspecialchars_decode ($ text , ENT_QUOTES );
4955 }
5056
5157 /**
5258 * @form yii1
59+ *
5360 * @param array $data data to be encoded
5461 * @param string $charset
62+ *
5563 * @return array the encoded data
56- * @see http://www.php.net/manual/en/function.htmlspecialchars.php
64+ * @see http://www.php.net/manual/en/function.htmlspecialchars.php
5765 */
58- public static function encodeArray ($ data , $ charset = 'utf-8 ' ): array
66+ public static function encodeArray ($ data , string $ charset = 'utf-8 ' ): array
5967 {
6068 $ d = [];
6169
@@ -84,12 +92,14 @@ public static function encodeArray($data, $charset = 'utf-8'): array
8492 * htmlentities() <--> html_entity_decode() — 将特殊的 HTML 实体转换回普通字符
8593 * htmlspecialchars() <--> htmlspecialchars_decode() — 将特殊的 HTML 实体转换回普通字符
8694 * ENT_COMPAT ENT_QUOTES ENT_NOQUOTES ENT_HTML401 ENT_XML1 ENT_XHTML ENT_HTML5
95+ *
8796 * @param $data
8897 * @param int $type
8998 * @param string $encoding
99+ *
90100 * @return array|mixed|string
91101 */
92- public static function escape ($ data , int $ type = 0 , $ encoding = 'UTF-8 ' )
102+ public static function escape ($ data , int $ type = 0 , string $ encoding = 'UTF-8 ' )
93103 {
94104 if (is_array ($ data )) {
95105 foreach ($ data as $ k => $ v ) {
@@ -108,26 +118,28 @@ public static function escape($data, int $type = 0, $encoding = 'UTF-8')
108118
109119 //如‘志’这样的16进制的html字符,为了防止这样的字符被错误转译,使用正则进行匹配,把这样的字符又转换回来。
110120 if (strpos ($ data , '&# ' )) {
111- $ data = \ preg_replace ('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/ ' , '& \\1 ' , $ data );
121+ $ data = preg_replace ('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/ ' , '& \\1 ' , $ data );
112122 }
113123
114124 return $ data ;
115125 }
116126
117127 /**
118128 * 去掉html转义
119- * @param $data
120- * @param int $type
121- * @param string $encoding
129+ *
130+ * @param string|array $data
131+ * @param int $type
132+ * @param string $encoding
133+ *
122134 * @return array|string
123135 */
124- public static function unescap ($ data , $ type = 0 , $ encoding = 'UTF-8 ' )
136+ public static function unescap ($ data , $ type = 0 , string $ encoding = 'UTF-8 ' )
125137 {
126138 if (is_array ($ data )) {
127139 foreach ($ data as $ k => $ v ) {
128140 $ data [$ k ] = self ::unescap ($ data , $ type , $ encoding );
129141 }
130- } elseif (!$ type ) {//默认使用 htmlspecialchars_decode()
142+ } elseif (!$ type ) {// 默认使用 htmlspecialchars_decode()
131143 $ data = htmlspecialchars_decode ($ data , \ENT_QUOTES );
132144 } else {
133145 $ data = html_entity_decode ($ data , \ENT_QUOTES , $ encoding );
@@ -138,7 +150,9 @@ public static function unescap($data, $type = 0, $encoding = 'UTF-8')
138150
139151 /**
140152 * Strip img-tags from string
141- * @param string $string Sting to be cleaned.
153+ *
154+ * @param string $string Sting to be cleaned.
155+ *
142156 * @return string Cleaned string
143157 */
144158 public static function stripImages (string $ string ): string
@@ -148,37 +162,44 @@ public static function stripImages(string $string): string
148162
149163 /**
150164 * Strip iframe-tags from string
151- * @param string $string Sting to be cleaned.
165+ *
166+ * @param string $string Sting to be cleaned.
167+ *
152168 * @return string Cleaned string
153169 */
154170 public static function stripIframes (string $ string ): string
155171 {
156- return \ preg_replace ('#(<[/]?iframe.*>)#U ' , '' , $ string );
172+ return preg_replace ('#(<[/]?iframe.*>)#U ' , '' , $ string );
157173 }
158174
159175 /**
160176 * stripScript
177+ *
161178 * @param string $string
179+ *
162180 * @return string
163181 */
164182 public static function stripScript (string $ string ): string
165183 {
166- return \ preg_replace ('/<script[^>]*>.*?</script>/si ' , '' , $ string );
184+ return preg_replace ('/<script[^>]*>.*?</script>/si ' , '' , $ string );
167185 }
168186
169187 /**
170188 * stripStyle
189+ *
171190 * @param string $string
191+ *
172192 * @return string
173193 */
174194 public static function stripStyle (string $ string ): string
175195 {
176- return \ preg_replace ('/<style[^>]*>.*?</style>/si ' , '' , $ string );
196+ return preg_replace ('/<style[^>]*>.*?</style>/si ' , '' , $ string );
177197 }
178198
179199 /**
180200 * @param string $html
181201 * @param bool|true $onlySrc
202+ *
182203 * @return array
183204 */
184205 public static function matchImages (string $ html , bool $ onlySrc = true ): array
@@ -199,6 +220,7 @@ public static function matchImages(string $html, bool $onlySrc = true): array
199220
200221 /**
201222 * @param string $html
223+ *
202224 * @return string
203225 */
204226 public static function minify (string $ html ): string
@@ -212,6 +234,6 @@ public static function minify(string $html): string
212234 ];
213235 $ replace = [' ' , ' ' , '> ' , '< ' , '\\1 ' ];
214236
215- return \ preg_replace ($ search , $ replace , $ html );
237+ return preg_replace ($ search , $ replace , $ html );
216238 }
217239}
0 commit comments