|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: Inhere |
| 5 | + * Date: 2016/8/10 0010 |
| 6 | + * Time: 00:44 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace MyLib\StrUtil; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class HtmlHelper |
| 13 | + * @package MyLib\StrUtil |
| 14 | + */ |
| 15 | +class HtmlHelper |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Encodes special characters into HTML entities. |
| 19 | + * @param string $text data to be encoded |
| 20 | + * @param string $charset |
| 21 | + * @return string the encoded data |
| 22 | + * @see http://www.php.net/manual/en/function.htmlspecialchars.php |
| 23 | + */ |
| 24 | + public static function encode($text, $charset = 'utf-8') |
| 25 | + { |
| 26 | + return htmlspecialchars($text, ENT_QUOTES, $charset); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * This is the opposite of {@link encode()}. |
| 31 | + * @param string $text data to be decoded |
| 32 | + * @return string the decoded data |
| 33 | + * @see http://www.php.net/manual/en/function.htmlspecialchars-decode.php |
| 34 | + */ |
| 35 | + public static function decode($text) |
| 36 | + { |
| 37 | + return htmlspecialchars_decode($text, ENT_QUOTES); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @form yii1 |
| 42 | + * @param array $data data to be encoded |
| 43 | + * @param string $charset |
| 44 | + * @return array the encoded data |
| 45 | + * @see http://www.php.net/manual/en/function.htmlspecialchars.php |
| 46 | + */ |
| 47 | + public static function encodeArray($data, $charset = 'utf-8') |
| 48 | + { |
| 49 | + $d = []; |
| 50 | + |
| 51 | + foreach ($data as $key => $value) { |
| 52 | + if (\is_string($key)) { |
| 53 | + $key = htmlspecialchars($key, ENT_QUOTES, $charset); |
| 54 | + } |
| 55 | + |
| 56 | + if (\is_string($value)) { |
| 57 | + $value = htmlspecialchars($value, ENT_QUOTES, $charset); |
| 58 | + } elseif (\is_array($value)) { |
| 59 | + $value = static::encodeArray($value); |
| 60 | + } |
| 61 | + |
| 62 | + $d[$key] = $value; |
| 63 | + } |
| 64 | + |
| 65 | + return $d; |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * html代码转义 |
| 71 | + * htmlspecialchars 只转化这几个html [ & ' " < > ] 代码 --> [ & " ], |
| 72 | + * 而 htmlentities 却会转化所有的html代码,连同里面的它无法识别的中文字符也会转化。 |
| 73 | + * 一般使用 htmlspecialchars 就足够了,要使用 htmlentities 时,要注意为第三个参数传递正确的编码。 |
| 74 | + * htmlentities() <--> html_entity_decode() — 将特殊的 HTML 实体转换回普通字符 |
| 75 | + * htmlspecialchars() <--> htmlspecialchars_decode() — 将特殊的 HTML 实体转换回普通字符 |
| 76 | + * ENT_COMPAT ENT_QUOTES ENT_NOQUOTES ENT_HTML401 ENT_XML1 ENT_XHTML ENT_HTML5 |
| 77 | + * @param $data |
| 78 | + * @param int $type |
| 79 | + * @param string $encoding |
| 80 | + * @return array|mixed|string |
| 81 | + */ |
| 82 | + public static function escape($data, int $type = 0, $encoding = 'UTF-8') |
| 83 | + { |
| 84 | + if (\is_array($data)) { |
| 85 | + foreach ($data as $k => $v) { |
| 86 | + $data[$k] = self::escape($data, $type, $encoding); |
| 87 | + } |
| 88 | + |
| 89 | + } else { |
| 90 | + // 默认使用 htmlspecialchars() |
| 91 | + if (!$type) { |
| 92 | + $data = htmlspecialchars($data, ENT_QUOTES, $encoding); |
| 93 | + } else { |
| 94 | + $data = htmlentities($data, ENT_QUOTES, $encoding); |
| 95 | + } |
| 96 | + |
| 97 | + //如‘志’这样的16进制的html字符,为了防止这样的字符被错误转译,使用正则进行匹配,把这样的字符又转换回来。 |
| 98 | + if (strpos($data, '&#')) { |
| 99 | + $data = preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4}));)/', |
| 100 | + '&\\1', $data); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $data; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * 去掉html转义 |
| 109 | + * @param $data |
| 110 | + * @param int $type |
| 111 | + * @param string $encoding |
| 112 | + * @return array|string |
| 113 | + */ |
| 114 | + public static function unescap($data, $type = 0, $encoding = 'UTF-8') |
| 115 | + { |
| 116 | + if (\is_array($data)) { |
| 117 | + foreach ($data as $k => $v) { |
| 118 | + $data[$k] = self::unescap($data, $type, $encoding); |
| 119 | + } |
| 120 | + |
| 121 | + } else { |
| 122 | + if (!$type) {//默认使用 htmlspecialchars_decode() |
| 123 | + $data = htmlspecialchars_decode($data, ENT_QUOTES); |
| 124 | + } else { |
| 125 | + $data = html_entity_decode($data, ENT_QUOTES, $encoding); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return $data; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Strip img-tags from string |
| 134 | + * @param string $string Sting to be cleaned. |
| 135 | + * @return string Cleaned string |
| 136 | + */ |
| 137 | + public static function stripImages(string $string): string |
| 138 | + { |
| 139 | + return preg_replace('#(<[/]?img.*>)#U', '', $string); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Strip iframe-tags from string |
| 144 | + * @param string $string Sting to be cleaned. |
| 145 | + * @return string Cleaned string |
| 146 | + */ |
| 147 | + public static function stripIframes(string $string): string |
| 148 | + { |
| 149 | + return preg_replace('#(<[/]?iframe.*>)#U', '', $string); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * stripScript |
| 154 | + * @param string $string |
| 155 | + * @return string |
| 156 | + */ |
| 157 | + public static function stripScript(string $string): string |
| 158 | + { |
| 159 | + return preg_replace('/<script[^>]*>.*?</script>/si', '', $string); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * stripStyle |
| 164 | + * @param string $string |
| 165 | + * @return string |
| 166 | + */ |
| 167 | + public static function stripStyle(string $string): string |
| 168 | + { |
| 169 | + return preg_replace('/<style[^>]*>.*?</style>/si', '', $string); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * @param string $html |
| 174 | + * @param bool|true $onlySrc |
| 175 | + * @return array |
| 176 | + */ |
| 177 | + public static function findImages(string $html, bool $onlySrc = true): array |
| 178 | + { |
| 179 | + // $preg = '/<img.*?src=[\"|\']?(.*?)[\"|\']?\s.*>/i'; |
| 180 | + $preg = '/<img.+src=\"(:?.+.+\.(?:jpg|gif|bmp|bnp|png)\"?).+>/i'; |
| 181 | + |
| 182 | + if (!preg_match_all($preg, trim($html), $images)) { |
| 183 | + return []; |
| 184 | + } |
| 185 | + |
| 186 | + if ($onlySrc) { |
| 187 | + return array_key_exists(1, $images) ? $images[1] : []; |
| 188 | + } |
| 189 | + |
| 190 | + return $images; |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * @param string $html |
| 195 | + * @return string |
| 196 | + */ |
| 197 | + public static function minify(string $html): string |
| 198 | + { |
| 199 | + $search = [ |
| 200 | + '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\')\/\/.*))/', |
| 201 | + '/\n/', |
| 202 | + '/\>[^\S ]+/s', |
| 203 | + '/[^\S ]+\</s', |
| 204 | + '/(\s)+/s' |
| 205 | + ]; |
| 206 | + $replace = [' ', ' ', '>', '<', '\\1']; |
| 207 | + |
| 208 | + return preg_replace($search, $replace, $html); |
| 209 | + } |
| 210 | +} |
0 commit comments