| @@ -75,8 +75,9 @@ | ||
| 75 | 75 | private const SIMPLE_CASE_FOLD = [['µ', 'ſ', "ͅ", 'ς', "ϐ", "ϑ", "ϕ", "ϖ", "ϰ", "ϱ", "ϵ", "ẛ", "ι"], ['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "ṡ", 'ι']]; |
| 76 | 76 | private static $encodingList = ['ASCII', 'UTF-8']; |
| 77 | 77 | private static $language = 'neutral'; |
| 78 | 78 | private static $internalEncoding = 'UTF-8'; |
| 79 | + private static $iconvSupportsIgnore; | |
| 79 | 80 | public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null) |
| 80 | 81 | { |
| 81 | 82 | if (\is_array($s)) { |
| 82 | 83 | $r = []; |
| @@ -84,9 +85,9 @@ | ||
| 84 | 85 | $r[] = self::mb_convert_encoding($str, $toEncoding, $fromEncoding); |
| 85 | 86 | } |
| 86 | 87 | return $r; |
| 87 | 88 | } |
| 88 | - if (\is_array($fromEncoding) || null !== $fromEncoding && \false !== \strpos($fromEncoding, ',')) { | |
| 89 | + if (\is_array($fromEncoding) || null !== $fromEncoding && \false !== strpos($fromEncoding, ',')) { | |
| 89 | 90 | $fromEncoding = self::mb_detect_encoding($s, $fromEncoding); |
| 90 | 91 | } else { |
| 91 | 92 | $fromEncoding = self::getEncoding($fromEncoding); |
| 92 | 93 | } |
| @@ -91,13 +92,13 @@ | ||
| 91 | 92 | $fromEncoding = self::getEncoding($fromEncoding); |
| 92 | 93 | } |
| 93 | 94 | $toEncoding = self::getEncoding($toEncoding); |
| 94 | 95 | if ('BASE64' === $fromEncoding) { |
| 95 | - $s = \base64_decode($s); | |
| 96 | + $s = base64_decode($s); | |
| 96 | 97 | $fromEncoding = $toEncoding; |
| 97 | 98 | } |
| 98 | 99 | if ('BASE64' === $toEncoding) { |
| 99 | - return \base64_encode($s); | |
| 100 | + return base64_encode($s); | |
| 100 | 101 | } |
| 101 | 102 | if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) { |
| 102 | 103 | if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) { |
| 103 | 104 | $fromEncoding = 'Windows-1252'; |
| @@ -102,23 +103,49 @@ | ||
| 102 | 103 | if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) { |
| 103 | 104 | $fromEncoding = 'Windows-1252'; |
| 104 | 105 | } |
| 105 | 106 | if ('UTF-8' !== $fromEncoding) { |
| 106 | - $s = \iconv($fromEncoding, 'UTF-8//IGNORE', $s); | |
| 107 | + $s = self::iconv($fromEncoding, 'UTF-8', $s); | |
| 107 | 108 | } |
| 108 | - return \preg_replace_callback('/[\\x80-\\xFF]+/', [__CLASS__, 'html_encoding_callback'], $s); | |
| 109 | + return preg_replace_callback('/[\x80-\xFF]+/', [__CLASS__, 'html_encoding_callback'], $s); | |
| 109 | 110 | } |
| 110 | 111 | if ('HTML-ENTITIES' === $fromEncoding) { |
| 111 | - $s = \html_entity_decode($s, \ENT_COMPAT, 'UTF-8'); | |
| 112 | + $decodeControlChars = static function ($m) { | |
| 113 | + $code = '' !== ($m[2] ?? '') ? hexdec($m[2]) : (int) $m[1]; | |
| 114 | + if ($code < 32 || 127 === $code) { | |
| 115 | + return \chr($code); | |
| 116 | + } | |
| 117 | + if (128 <= $code && $code <= 159) { | |
| 118 | + return "\xc2" . \chr(0x80 | $code & 0x3f); | |
| 119 | + } | |
| 120 | + return $m[0]; | |
| 121 | + }; | |
| 122 | + if (\PHP_VERSION_ID >= 70400) { | |
| 123 | + $s = html_entity_decode($s, \ENT_QUOTES, 'UTF-8'); | |
| 124 | + // html_entity_decode() leaves numeric entities for C0/C1 control | |
| 125 | + // characters as-is (HTML spec), but mb_convert_encoding() decodes | |
| 126 | + // them. Catch what html_entity_decode() missed. | |
| 127 | + if (\false !== strpos($s, '&#')) { | |
| 128 | + $s = preg_replace_callback('/&#(?:0*([0-9]++)|[xX]0*([0-9a-fA-F]++));/', $decodeControlChars, $s); | |
| 129 | + } | |
| 130 | + } else { | |
| 131 | + // PHP < 7.4: html_entity_decode() truncates strings at NUL bytes, | |
| 132 | + // so decode the control character entities first then call | |
| 133 | + // html_entity_decode() on each NUL-delimited chunk independently. | |
| 134 | + $s = preg_replace_callback('/&#(?:0*([0-9]++)|[xX]0*([0-9a-fA-F]++));/', $decodeControlChars, $s); | |
| 135 | + $s = implode("\x00", array_map(static function ($chunk) { | |
| 136 | + return html_entity_decode($chunk, \ENT_QUOTES, 'UTF-8'); | |
| 137 | + }, explode("\x00", $s))); | |
| 138 | + } | |
| 112 | 139 | $fromEncoding = 'UTF-8'; |
| 113 | 140 | } |
| 114 | - return \iconv($fromEncoding, $toEncoding . '//IGNORE', $s); | |
| 141 | + return self::iconv($fromEncoding, $toEncoding, $s); | |
| 115 | 142 | } |
| 116 | 143 | public static function mb_convert_variables($toEncoding, $fromEncoding, &...$vars) |
| 117 | 144 | { |
| 118 | 145 | $ok = \true; |
| 119 | - \array_walk_recursive($vars, function (&$v) use(&$ok, $toEncoding, $fromEncoding) { | |
| 120 | - if (\false === ($v = self::mb_convert_encoding($v, $toEncoding, $fromEncoding))) { | |
| 146 | + array_walk_recursive($vars, static function (&$v) use (&$ok, $toEncoding, $fromEncoding) { | |
| 147 | + if (\false === $v = self::mb_convert_encoding($v, $toEncoding, $fromEncoding)) { | |
| 121 | 148 | $ok = \false; |
| 122 | 149 | } |
| 123 | 150 | }); |
| 124 | 151 | return $ok ? $fromEncoding : \false; |
| @@ -124,18 +151,18 @@ | ||
| 124 | 151 | return $ok ? $fromEncoding : \false; |
| 125 | 152 | } |
| 126 | 153 | public static function mb_decode_mimeheader($s) |
| 127 | 154 | { |
| 128 | - return \iconv_mime_decode($s, 2, self::$internalEncoding); | |
| 155 | + return iconv_mime_decode($s, 2, self::$internalEncoding); | |
| 129 | 156 | } |
| 130 | 157 | public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null) |
| 131 | 158 | { |
| 132 | - \trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', \E_USER_WARNING); | |
| 159 | + trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', \E_USER_WARNING); | |
| 133 | 160 | } |
| 134 | 161 | public static function mb_decode_numericentity($s, $convmap, $encoding = null) |
| 135 | 162 | { |
| 136 | - if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) { | |
| 137 | - \trigger_error('mb_decode_numericentity() expects parameter 1 to be string, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 163 | + if (null !== $s && !\is_scalar($s) && !(\is_object($s) && method_exists($s, '__toString'))) { | |
| 164 | + trigger_error('mb_decode_numericentity() expects parameter 1 to be string, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 138 | 165 | return null; |
| 139 | 166 | } |
| 140 | 167 | if (!\is_array($convmap) || 80000 > \PHP_VERSION_ID && !$convmap) { |
| 141 | 168 | return \false; |
| @@ -140,9 +167,9 @@ | ||
| 140 | 167 | if (!\is_array($convmap) || 80000 > \PHP_VERSION_ID && !$convmap) { |
| 141 | 168 | return \false; |
| 142 | 169 | } |
| 143 | 170 | if (null !== $encoding && !\is_scalar($encoding)) { |
| 144 | - \trigger_error('mb_decode_numericentity() expects parameter 3 to be string, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 171 | + trigger_error('mb_decode_numericentity() expects parameter 3 to be string, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 145 | 172 | return ''; |
| 146 | 173 | // Instead of null (cf. mb_encode_numericentity). |
| 147 | 174 | } |
| 148 | 175 | $s = (string) $s; |
| @@ -151,22 +178,22 @@ | ||
| 151 | 178 | } |
| 152 | 179 | $encoding = self::getEncoding($encoding); |
| 153 | 180 | if ('UTF-8' === $encoding) { |
| 154 | 181 | $encoding = null; |
| 155 | - if (!\preg_match('//u', $s)) { | |
| 156 | - $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 182 | + if (!preg_match('//u', $s)) { | |
| 183 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 157 | 184 | } |
| 158 | 185 | } else { |
| 159 | - $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 186 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 160 | 187 | } |
| 161 | - $cnt = \floor(\count($convmap) / 4) * 4; | |
| 188 | + $cnt = floor(\count($convmap) / 4) * 4; | |
| 162 | 189 | for ($i = 0; $i < $cnt; $i += 4) { |
| 163 | 190 | // collector_decode_htmlnumericentity ignores $convmap[$i + 3] |
| 164 | 191 | $convmap[$i] += $convmap[$i + 2]; |
| 165 | 192 | $convmap[$i + 1] += $convmap[$i + 2]; |
| 166 | 193 | } |
| 167 | - $s = \preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use($cnt, $convmap) { | |
| 168 | - $c = isset($m[2]) ? (int) \hexdec($m[2]) : $m[1]; | |
| 194 | + $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))' . (\PHP_VERSION_ID >= 80200 ? '' : '(?!&)') . ';?/', static function (array $m) use ($cnt, $convmap) { | |
| 195 | + $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1]; | |
| 169 | 196 | for ($i = 0; $i < $cnt; $i += 4) { |
| 170 | 197 | if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) { |
| 171 | 198 | return self::mb_chr($c - $convmap[$i + 2]); |
| 172 | 199 | } |
| @@ -175,14 +202,14 @@ | ||
| 175 | 202 | }, $s); |
| 176 | 203 | if (null === $encoding) { |
| 177 | 204 | return $s; |
| 178 | 205 | } |
| 179 | - return \iconv('UTF-8', $encoding . '//IGNORE', $s); | |
| 206 | + return self::iconv('UTF-8', $encoding, $s); | |
| 180 | 207 | } |
| 181 | 208 | public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = \false) |
| 182 | 209 | { |
| 183 | - if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) { | |
| 184 | - \trigger_error('mb_encode_numericentity() expects parameter 1 to be string, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 210 | + if (null !== $s && !\is_scalar($s) && !(\is_object($s) && method_exists($s, '__toString'))) { | |
| 211 | + trigger_error('mb_encode_numericentity() expects parameter 1 to be string, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 185 | 212 | return null; |
| 186 | 213 | } |
| 187 | 214 | if (!\is_array($convmap) || 80000 > \PHP_VERSION_ID && !$convmap) { |
| 188 | 215 | return \false; |
| @@ -187,14 +214,14 @@ | ||
| 187 | 214 | if (!\is_array($convmap) || 80000 > \PHP_VERSION_ID && !$convmap) { |
| 188 | 215 | return \false; |
| 189 | 216 | } |
| 190 | 217 | if (null !== $encoding && !\is_scalar($encoding)) { |
| 191 | - \trigger_error('mb_encode_numericentity() expects parameter 3 to be string, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 218 | + trigger_error('mb_encode_numericentity() expects parameter 3 to be string, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 192 | 219 | return null; |
| 193 | 220 | // Instead of '' (cf. mb_decode_numericentity). |
| 194 | 221 | } |
| 195 | 222 | if (null !== $is_hex && !\is_scalar($is_hex)) { |
| 196 | - \trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 223 | + trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, ' . \gettype($s) . ' given', \E_USER_WARNING); | |
| 197 | 224 | return null; |
| 198 | 225 | } |
| 199 | 226 | $s = (string) $s; |
| 200 | 227 | if ('' === $s) { |
| @@ -202,22 +229,22 @@ | ||
| 202 | 229 | } |
| 203 | 230 | $encoding = self::getEncoding($encoding); |
| 204 | 231 | if ('UTF-8' === $encoding) { |
| 205 | 232 | $encoding = null; |
| 206 | - if (!\preg_match('//u', $s)) { | |
| 207 | - $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 233 | + if (!preg_match('//u', $s)) { | |
| 234 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 208 | 235 | } |
| 209 | 236 | } else { |
| 210 | - $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 237 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 211 | 238 | } |
| 212 | 239 | static $ulenMask = ["\xc0" => 2, "\xd0" => 2, "\xe0" => 3, "\xf0" => 4]; |
| 213 | - $cnt = \floor(\count($convmap) / 4) * 4; | |
| 240 | + $cnt = floor(\count($convmap) / 4) * 4; | |
| 214 | 241 | $i = 0; |
| 215 | 242 | $len = \strlen($s); |
| 216 | 243 | $result = ''; |
| 217 | 244 | while ($i < $len) { |
| 218 | 245 | $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xf0"]; |
| 219 | - $uchr = \substr($s, $i, $ulen); | |
| 246 | + $uchr = substr($s, $i, $ulen); | |
| 220 | 247 | $i += $ulen; |
| 221 | 248 | $c = self::mb_ord($uchr); |
| 222 | 249 | for ($j = 0; $j < $cnt; $j += 4) { |
| 223 | 250 | if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) { |
| @@ -230,9 +257,9 @@ | ||
| 230 | 257 | } |
| 231 | 258 | if (null === $encoding) { |
| 232 | 259 | return $result; |
| 233 | 260 | } |
| 234 | - return \iconv('UTF-8', $encoding . '//IGNORE', $result); | |
| 261 | + return self::iconv('UTF-8', $encoding, $result); | |
| 235 | 262 | } |
| 236 | 263 | public static function mb_convert_case($s, $mode, $encoding = null) |
| 237 | 264 | { |
| 238 | 265 | $s = (string) $s; |
| @@ -241,13 +268,13 @@ | ||
| 241 | 268 | } |
| 242 | 269 | $encoding = self::getEncoding($encoding); |
| 243 | 270 | if ('UTF-8' === $encoding) { |
| 244 | 271 | $encoding = null; |
| 245 | - if (!\preg_match('//u', $s)) { | |
| 246 | - $s = @\iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 272 | + if (!preg_match('//u', $s)) { | |
| 273 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 247 | 274 | } |
| 248 | 275 | } else { |
| 249 | - $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 276 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 250 | 277 | } |
| 251 | 278 | if (\MB_CASE_TITLE == $mode) { |
| 252 | 279 | static $titleRegexp = null; |
| 253 | 280 | if (null === $titleRegexp) { |
| @@ -252,9 +279,9 @@ | ||
| 252 | 279 | static $titleRegexp = null; |
| 253 | 280 | if (null === $titleRegexp) { |
| 254 | 281 | $titleRegexp = self::getData('titleCaseRegexp'); |
| 255 | 282 | } |
| 256 | - $s = \preg_replace_callback($titleRegexp, [__CLASS__, 'title_case'], $s); | |
| 283 | + $s = preg_replace_callback($titleRegexp, [__CLASS__, 'title_case'], $s); | |
| 257 | 284 | } else { |
| 258 | 285 | if (\MB_CASE_UPPER == $mode) { |
| 259 | 286 | static $upper = null; |
| 260 | 287 | if (null === $upper) { |
| @@ -266,9 +293,9 @@ | ||
| 266 | 293 | static $caseFolding = null; |
| 267 | 294 | if (null === $caseFolding) { |
| 268 | 295 | $caseFolding = self::getData('caseFolding'); |
| 269 | 296 | } |
| 270 | - $s = \strtr($s, $caseFolding); | |
| 297 | + $s = strtr($s, $caseFolding); | |
| 271 | 298 | } |
| 272 | 299 | static $lower = null; |
| 273 | 300 | if (null === $lower) { |
| 274 | 301 | $lower = self::getData('lowerCase'); |
| @@ -279,9 +306,9 @@ | ||
| 279 | 306 | $i = 0; |
| 280 | 307 | $len = \strlen($s); |
| 281 | 308 | while ($i < $len) { |
| 282 | 309 | $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xf0"]; |
| 283 | - $uchr = \substr($s, $i, $ulen); | |
| 310 | + $uchr = substr($s, $i, $ulen); | |
| 284 | 311 | $i += $ulen; |
| 285 | 312 | if (isset($map[$uchr])) { |
| 286 | 313 | $uchr = $map[$uchr]; |
| 287 | 314 | $nlen = \strlen($uchr); |
| @@ -290,9 +317,9 @@ | ||
| 290 | 317 | do { |
| 291 | 318 | $s[--$nlen] = $uchr[--$ulen]; |
| 292 | 319 | } while ($ulen); |
| 293 | 320 | } else { |
| 294 | - $s = \substr_replace($s, $uchr, $i - $ulen, $ulen); | |
| 321 | + $s = substr_replace($s, $uchr, $i - $ulen, $ulen); | |
| 295 | 322 | $len += $nlen - $ulen; |
| 296 | 323 | $i += $nlen - $ulen; |
| 297 | 324 | } |
| 298 | 325 | } |
| @@ -300,9 +327,9 @@ | ||
| 300 | 327 | } |
| 301 | 328 | if (null === $encoding) { |
| 302 | 329 | return $s; |
| 303 | 330 | } |
| 304 | - return \iconv('UTF-8', $encoding . '//IGNORE', $s); | |
| 331 | + return self::iconv('UTF-8', $encoding, $s); | |
| 305 | 332 | } |
| 306 | 333 | public static function mb_internal_encoding($encoding = null) |
| 307 | 334 | { |
| 308 | 335 | if (null === $encoding) { |
| @@ -308,9 +335,9 @@ | ||
| 308 | 335 | if (null === $encoding) { |
| 309 | 336 | return self::$internalEncoding; |
| 310 | 337 | } |
| 311 | 338 | $normalizedEncoding = self::getEncoding($encoding); |
| 312 | - if ('UTF-8' === $normalizedEncoding || \false !== @\iconv($normalizedEncoding, $normalizedEncoding, ' ')) { | |
| 339 | + if ('UTF-8' === $normalizedEncoding || \false !== @iconv($normalizedEncoding, $normalizedEncoding, ' ')) { | |
| 313 | 340 | self::$internalEncoding = $normalizedEncoding; |
| 314 | 341 | return \true; |
| 315 | 342 | } |
| 316 | 343 | if (80000 > \PHP_VERSION_ID) { |
| @@ -322,9 +349,9 @@ | ||
| 322 | 349 | { |
| 323 | 350 | if (null === $lang) { |
| 324 | 351 | return self::$language; |
| 325 | 352 | } |
| 326 | - switch ($normalizedLang = \strtolower($lang)) { | |
| 353 | + switch ($normalizedLang = strtolower($lang)) { | |
| 327 | 354 | case 'uni': |
| 328 | 355 | case 'neutral': |
| 329 | 356 | self::$language = $normalizedLang; |
| 330 | 357 | return \true; |
| @@ -339,9 +366,9 @@ | ||
| 339 | 366 | return ['UTF-8']; |
| 340 | 367 | } |
| 341 | 368 | public static function mb_encoding_aliases($encoding) |
| 342 | 369 | { |
| 343 | - switch (\strtoupper($encoding)) { | |
| 370 | + switch (strtoupper($encoding)) { | |
| 344 | 371 | case 'UTF8': |
| 345 | 372 | case 'UTF-8': |
| 346 | 373 | return ['utf8']; |
| 347 | 374 | } |
| @@ -355,9 +382,9 @@ | ||
| 355 | 382 | } |
| 356 | 383 | $encoding = self::$internalEncoding; |
| 357 | 384 | } |
| 358 | 385 | if (!\is_array($var)) { |
| 359 | - return self::mb_detect_encoding($var, [$encoding]) || \false !== @\iconv($encoding, $encoding, $var); | |
| 386 | + return self::mb_detect_encoding($var, [$encoding]) || \false !== @iconv($encoding, $encoding, $var); | |
| 360 | 387 | } |
| 361 | 388 | foreach ($var as $key => $value) { |
| 362 | 389 | if (!self::mb_check_encoding($key, $encoding)) { |
| 363 | 390 | return \false; |
| @@ -373,27 +400,27 @@ | ||
| 373 | 400 | if (null === $encodingList) { |
| 374 | 401 | $encodingList = self::$encodingList; |
| 375 | 402 | } else { |
| 376 | 403 | if (!\is_array($encodingList)) { |
| 377 | - $encodingList = \array_map('trim', \explode(',', $encodingList)); | |
| 404 | + $encodingList = array_map('trim', explode(',', $encodingList)); | |
| 378 | 405 | } |
| 379 | - $encodingList = \array_map('strtoupper', $encodingList); | |
| 406 | + $encodingList = array_map('strtoupper', $encodingList); | |
| 380 | 407 | } |
| 381 | 408 | foreach ($encodingList as $enc) { |
| 382 | 409 | switch ($enc) { |
| 383 | 410 | case 'ASCII': |
| 384 | - if (!\preg_match('/[\\x80-\\xFF]/', $str)) { | |
| 411 | + if (!preg_match('/[\x80-\xFF]/', $str)) { | |
| 385 | 412 | return $enc; |
| 386 | 413 | } |
| 387 | 414 | break; |
| 388 | 415 | case 'UTF8': |
| 389 | 416 | case 'UTF-8': |
| 390 | - if (\preg_match('//u', $str)) { | |
| 417 | + if (preg_match('//u', $str)) { | |
| 391 | 418 | return 'UTF-8'; |
| 392 | 419 | } |
| 393 | 420 | break; |
| 394 | 421 | default: |
| 395 | - if (0 === \strncmp($enc, 'ISO-8859-', 9)) { | |
| 422 | + if (0 === strncmp($enc, 'ISO-8859-', 9)) { | |
| 396 | 423 | return $enc; |
| 397 | 424 | } |
| 398 | 425 | } |
| 399 | 426 | } |
| @@ -404,15 +431,15 @@ | ||
| 404 | 431 | if (null === $encodingList) { |
| 405 | 432 | return self::$encodingList; |
| 406 | 433 | } |
| 407 | 434 | if (!\is_array($encodingList)) { |
| 408 | - $encodingList = \array_map('trim', \explode(',', $encodingList)); | |
| 435 | + $encodingList = array_map('trim', explode(',', $encodingList)); | |
| 409 | 436 | } |
| 410 | - $encodingList = \array_map('strtoupper', $encodingList); | |
| 437 | + $encodingList = array_map('strtoupper', $encodingList); | |
| 411 | 438 | foreach ($encodingList as $enc) { |
| 412 | 439 | switch ($enc) { |
| 413 | 440 | default: |
| 414 | - if (\strncmp($enc, 'ISO-8859-', 9)) { | |
| 441 | + if (strncmp($enc, 'ISO-8859-', 9)) { | |
| 415 | 442 | return \false; |
| 416 | 443 | } |
| 417 | 444 | // no break |
| 418 | 445 | case 'ASCII': |
| @@ -428,37 +455,43 @@ | ||
| 428 | 455 | $encoding = self::getEncoding($encoding); |
| 429 | 456 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 430 | 457 | return \strlen($s); |
| 431 | 458 | } |
| 432 | - return @\iconv_strlen($s, $encoding); | |
| 459 | + if (\false !== $len = @iconv_strlen($s, $encoding)) { | |
| 460 | + return $len; | |
| 461 | + } | |
| 462 | + if ('UTF-8' !== $encoding) { | |
| 463 | + return $len; | |
| 464 | + } | |
| 465 | + return preg_match_all('/[\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]?|[\xE0-\xEF][\x80-\xBF]{0,2}|[\xF0-\xF7][\x80-\xBF]{0,3}|[\xF8-\xFB][\x80-\xBF]{0,4}|[\xFC-\xFD][\x80-\xBF]{0,5}|[\x80-\xBF\xFE\xFF]/s', $s); | |
| 433 | 466 | } |
| 434 | 467 | public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) |
| 435 | 468 | { |
| 436 | 469 | $encoding = self::getEncoding($encoding); |
| 437 | 470 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 438 | - return \strpos($haystack, $needle, $offset); | |
| 471 | + return strpos($haystack, $needle, $offset); | |
| 439 | 472 | } |
| 440 | 473 | $needle = (string) $needle; |
| 441 | 474 | if ('' === $needle) { |
| 442 | 475 | if (80000 > \PHP_VERSION_ID) { |
| 443 | - \trigger_error(__METHOD__ . ': Empty delimiter', \E_USER_WARNING); | |
| 476 | + trigger_error(__METHOD__ . ': Empty delimiter', \E_USER_WARNING); | |
| 444 | 477 | return \false; |
| 445 | 478 | } |
| 446 | 479 | return 0; |
| 447 | 480 | } |
| 448 | - return \iconv_strpos($haystack, $needle, $offset, $encoding); | |
| 481 | + return iconv_strpos($haystack, $needle, $offset, $encoding); | |
| 449 | 482 | } |
| 450 | 483 | public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null) |
| 451 | 484 | { |
| 452 | 485 | $encoding = self::getEncoding($encoding); |
| 453 | 486 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 454 | - return \strrpos($haystack, $needle, $offset); | |
| 487 | + return strrpos($haystack, $needle, $offset); | |
| 455 | 488 | } |
| 456 | 489 | if ($offset != (int) $offset) { |
| 457 | 490 | $offset = 0; |
| 458 | 491 | } elseif ($offset = (int) $offset) { |
| 459 | 492 | if ($offset < 0) { |
| 460 | - if (0 > ($offset += self::mb_strlen($needle))) { | |
| 493 | + if (0 > $offset += self::mb_strlen($needle)) { | |
| 461 | 494 | $haystack = self::mb_substr($haystack, 0, $offset, $encoding); |
| 462 | 495 | } |
| 463 | 496 | $offset = 0; |
| 464 | 497 | } else { |
| @@ -464,28 +497,28 @@ | ||
| 464 | 497 | } else { |
| 465 | 498 | $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding); |
| 466 | 499 | } |
| 467 | 500 | } |
| 468 | - $pos = '' !== $needle || 80000 > \PHP_VERSION_ID ? \iconv_strrpos($haystack, $needle, $encoding) : self::mb_strlen($haystack, $encoding); | |
| 501 | + $pos = '' !== $needle || 80000 > \PHP_VERSION_ID ? iconv_strrpos($haystack, $needle, $encoding) : self::mb_strlen($haystack, $encoding); | |
| 469 | 502 | return \false !== $pos ? $offset + $pos : \false; |
| 470 | 503 | } |
| 471 | 504 | public static function mb_str_split($string, $split_length = 1, $encoding = null) |
| 472 | 505 | { |
| 473 | - if (null !== $string && !\is_scalar($string) && !(\is_object($string) && \method_exists($string, '__toString'))) { | |
| 474 | - \trigger_error('mb_str_split() expects parameter 1 to be string, ' . \gettype($string) . ' given', \E_USER_WARNING); | |
| 506 | + if (null !== $string && !\is_scalar($string) && !(\is_object($string) && method_exists($string, '__toString'))) { | |
| 507 | + trigger_error('mb_str_split() expects parameter 1 to be string, ' . \gettype($string) . ' given', \E_USER_WARNING); | |
| 475 | 508 | return null; |
| 476 | 509 | } |
| 477 | - if (1 > ($split_length = (int) $split_length)) { | |
| 510 | + if (1 > $split_length = (int) $split_length) { | |
| 478 | 511 | if (80000 > \PHP_VERSION_ID) { |
| 479 | - \trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING); | |
| 512 | + trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING); | |
| 480 | 513 | return \false; |
| 481 | 514 | } |
| 482 | 515 | throw new \ValueError('Argument #2 ($length) must be greater than 0'); |
| 483 | 516 | } |
| 484 | 517 | if (null === $encoding) { |
| 485 | - $encoding = \mb_internal_encoding(); | |
| 518 | + $encoding = mb_internal_encoding(); | |
| 486 | 519 | } |
| 487 | - if ('UTF-8' === ($encoding = self::getEncoding($encoding))) { | |
| 520 | + if ('UTF-8' === $encoding = self::getEncoding($encoding)) { | |
| 488 | 521 | $rx = '/('; |
| 489 | 522 | while (65535 < $split_length) { |
| 490 | 523 | $rx .= '.{65535}'; |
| 491 | 524 | $split_length -= 65535; |
| @@ -490,14 +523,14 @@ | ||
| 490 | 523 | $rx .= '.{65535}'; |
| 491 | 524 | $split_length -= 65535; |
| 492 | 525 | } |
| 493 | 526 | $rx .= '.{' . $split_length . '})/us'; |
| 494 | - return \preg_split($rx, $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY); | |
| 527 | + return preg_split($rx, $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY); | |
| 495 | 528 | } |
| 496 | 529 | $result = []; |
| 497 | - $length = \mb_strlen($string, $encoding); | |
| 530 | + $length = mb_strlen($string, $encoding); | |
| 498 | 531 | for ($i = 0; $i < $length; $i += $split_length) { |
| 499 | - $result[] = \mb_substr($string, $i, $split_length, $encoding); | |
| 532 | + $result[] = mb_substr($string, $i, $split_length, $encoding); | |
| 500 | 533 | } |
| 501 | 534 | return $result; |
| 502 | 535 | } |
| 503 | 536 | public static function mb_strtolower($s, $encoding = null) |
| @@ -512,9 +545,9 @@ | ||
| 512 | 545 | { |
| 513 | 546 | if (null === $c) { |
| 514 | 547 | return 'none'; |
| 515 | 548 | } |
| 516 | - if (0 === \strcasecmp($c, 'none')) { | |
| 549 | + if (0 === strcasecmp($c, 'none')) { | |
| 517 | 550 | return \true; |
| 518 | 551 | } |
| 519 | 552 | if (80000 > \PHP_VERSION_ID) { |
| 520 | 553 | return \false; |
| @@ -527,12 +560,12 @@ | ||
| 527 | 560 | public static function mb_substr($s, $start, $length = null, $encoding = null) |
| 528 | 561 | { |
| 529 | 562 | $encoding = self::getEncoding($encoding); |
| 530 | 563 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 531 | - return (string) \substr($s, $start, null === $length ? 2147483647 : $length); | |
| 564 | + return (string) substr($s, $start, null === $length ? 2147483647 : $length); | |
| 532 | 565 | } |
| 533 | 566 | if ($start < 0) { |
| 534 | - $start = \iconv_strlen($s, $encoding) + $start; | |
| 567 | + $start = iconv_strlen($s, $encoding) + $start; | |
| 535 | 568 | if ($start < 0) { |
| 536 | 569 | $start = 0; |
| 537 | 570 | } |
| 538 | 571 | } |
| @@ -538,18 +571,18 @@ | ||
| 538 | 571 | } |
| 539 | 572 | if (null === $length) { |
| 540 | 573 | $length = 2147483647; |
| 541 | 574 | } elseif ($length < 0) { |
| 542 | - $length = \iconv_strlen($s, $encoding) + $length - $start; | |
| 575 | + $length = iconv_strlen($s, $encoding) + $length - $start; | |
| 543 | 576 | if ($length < 0) { |
| 544 | 577 | return ''; |
| 545 | 578 | } |
| 546 | 579 | } |
| 547 | - return (string) \iconv_substr($s, $start, $length, $encoding); | |
| 580 | + return (string) iconv_substr($s, $start, $length, $encoding); | |
| 548 | 581 | } |
| 549 | 582 | public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null) |
| 550 | 583 | { |
| 551 | - [$haystack, $needle] = \str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], [self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding), self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding)]); | |
| 584 | + [$haystack, $needle] = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], [self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding), self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding)]); | |
| 552 | 585 | return self::mb_strpos($haystack, $needle, $offset, $encoding); |
| 553 | 586 | } |
| 554 | 587 | public static function mb_stristr($haystack, $needle, $part = \false, $encoding = null) |
| 555 | 588 | { |
| @@ -559,12 +592,12 @@ | ||
| 559 | 592 | public static function mb_strrchr($haystack, $needle, $part = \false, $encoding = null) |
| 560 | 593 | { |
| 561 | 594 | $encoding = self::getEncoding($encoding); |
| 562 | 595 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 563 | - $pos = \strrpos($haystack, $needle); | |
| 596 | + $pos = strrpos($haystack, $needle); | |
| 564 | 597 | } else { |
| 565 | 598 | $needle = self::mb_substr($needle, 0, 1, $encoding); |
| 566 | - $pos = \iconv_strrpos($haystack, $needle, $encoding); | |
| 599 | + $pos = iconv_strrpos($haystack, $needle, $encoding); | |
| 567 | 600 | } |
| 568 | 601 | return self::getSubpart($pos, $part, $haystack, $encoding); |
| 569 | 602 | } |
| 570 | 603 | public static function mb_strrichr($haystack, $needle, $part = \false, $encoding = null) |
| @@ -576,26 +609,26 @@ | ||
| 576 | 609 | public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null) |
| 577 | 610 | { |
| 578 | 611 | $haystack = self::mb_convert_case($haystack, \MB_CASE_LOWER, $encoding); |
| 579 | 612 | $needle = self::mb_convert_case($needle, \MB_CASE_LOWER, $encoding); |
| 580 | - $haystack = \str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $haystack); | |
| 581 | - $needle = \str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $needle); | |
| 613 | + $haystack = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $haystack); | |
| 614 | + $needle = str_replace(self::SIMPLE_CASE_FOLD[0], self::SIMPLE_CASE_FOLD[1], $needle); | |
| 582 | 615 | return self::mb_strrpos($haystack, $needle, $offset, $encoding); |
| 583 | 616 | } |
| 584 | 617 | public static function mb_strstr($haystack, $needle, $part = \false, $encoding = null) |
| 585 | 618 | { |
| 586 | - $pos = \strpos($haystack, $needle); | |
| 619 | + $pos = strpos($haystack, $needle); | |
| 587 | 620 | if (\false === $pos) { |
| 588 | 621 | return \false; |
| 589 | 622 | } |
| 590 | 623 | if ($part) { |
| 591 | - return \substr($haystack, 0, $pos); | |
| 624 | + return substr($haystack, 0, $pos); | |
| 592 | 625 | } |
| 593 | - return \substr($haystack, $pos); | |
| 626 | + return substr($haystack, $pos); | |
| 594 | 627 | } |
| 595 | 628 | public static function mb_get_info($type = 'all') |
| 596 | 629 | { |
| 597 | - $info = ['internal_encoding' => self::$internalEncoding, 'http_output' => 'pass', 'http_output_conv_mimetypes' => '^(text/|application/xhtml\\+xml)', 'func_overload' => 0, 'func_overload_list' => 'no overload', 'mail_charset' => 'UTF-8', 'mail_header_encoding' => 'BASE64', 'mail_body_encoding' => 'BASE64', 'illegal_chars' => 0, 'encoding_translation' => 'Off', 'language' => self::$language, 'detect_order' => self::$encodingList, 'substitute_character' => 'none', 'strict_detection' => 'Off']; | |
| 630 | + $info = ['internal_encoding' => self::$internalEncoding, 'http_output' => 'pass', 'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)', 'func_overload' => 0, 'func_overload_list' => 'no overload', 'mail_charset' => 'UTF-8', 'mail_header_encoding' => 'BASE64', 'mail_body_encoding' => 'BASE64', 'illegal_chars' => 0, 'encoding_translation' => 'Off', 'language' => self::$language, 'detect_order' => self::$encodingList, 'substitute_character' => 'none', 'strict_detection' => 'Off']; | |
| 598 | 631 | if ('all' === $type) { |
| 599 | 632 | return $info; |
| 600 | 633 | } |
| 601 | 634 | if (isset($info[$type])) { |
| @@ -614,16 +647,16 @@ | ||
| 614 | 647 | public static function mb_strwidth($s, $encoding = null) |
| 615 | 648 | { |
| 616 | 649 | $encoding = self::getEncoding($encoding); |
| 617 | 650 | if ('UTF-8' !== $encoding) { |
| 618 | - $s = \iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 651 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 619 | 652 | } |
| 620 | - $s = \preg_replace('/[\\x{1100}-\\x{115F}\\x{2329}\\x{232A}\\x{2E80}-\\x{303E}\\x{3040}-\\x{A4CF}\\x{AC00}-\\x{D7A3}\\x{F900}-\\x{FAFF}\\x{FE10}-\\x{FE19}\\x{FE30}-\\x{FE6F}\\x{FF00}-\\x{FF60}\\x{FFE0}-\\x{FFE6}\\x{20000}-\\x{2FFFD}\\x{30000}-\\x{3FFFD}]/u', '', $s, -1, $wide); | |
| 621 | - return ($wide << 1) + \iconv_strlen($s, 'UTF-8'); | |
| 653 | + $s = preg_replace('/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', '', $s, -1, $wide); | |
| 654 | + return ($wide << 1) + iconv_strlen($s, 'UTF-8'); | |
| 622 | 655 | } |
| 623 | 656 | public static function mb_substr_count($haystack, $needle, $encoding = null) |
| 624 | 657 | { |
| 625 | - return \substr_count($haystack, $needle); | |
| 658 | + return substr_count($haystack, $needle); | |
| 626 | 659 | } |
| 627 | 660 | public static function mb_output_handler($contents, $status) |
| 628 | 661 | { |
| 629 | 662 | return $contents; |
| @@ -629,9 +662,9 @@ | ||
| 629 | 662 | return $contents; |
| 630 | 663 | } |
| 631 | 664 | public static function mb_chr($code, $encoding = null) |
| 632 | 665 | { |
| 633 | - if (0x80 > ($code %= 0x200000)) { | |
| 666 | + if (0x80 > $code %= 0x200000) { | |
| 634 | 667 | $s = \chr($code); |
| 635 | 668 | } elseif (0x800 > $code) { |
| 636 | 669 | $s = \chr(0xc0 | $code >> 6) . \chr(0x80 | $code & 0x3f); |
| 637 | 670 | } elseif (0x10000 > $code) { |
| @@ -638,22 +671,22 @@ | ||
| 638 | 671 | $s = \chr(0xe0 | $code >> 12) . \chr(0x80 | $code >> 6 & 0x3f) . \chr(0x80 | $code & 0x3f); |
| 639 | 672 | } else { |
| 640 | 673 | $s = \chr(0xf0 | $code >> 18) . \chr(0x80 | $code >> 12 & 0x3f) . \chr(0x80 | $code >> 6 & 0x3f) . \chr(0x80 | $code & 0x3f); |
| 641 | 674 | } |
| 642 | - if ('UTF-8' !== ($encoding = self::getEncoding($encoding))) { | |
| 643 | - $s = \mb_convert_encoding($s, $encoding, 'UTF-8'); | |
| 675 | + if ('UTF-8' !== $encoding = self::getEncoding($encoding)) { | |
| 676 | + $s = mb_convert_encoding($s, $encoding, 'UTF-8'); | |
| 644 | 677 | } |
| 645 | 678 | return $s; |
| 646 | 679 | } |
| 647 | 680 | public static function mb_ord($s, $encoding = null) |
| 648 | 681 | { |
| 649 | - if ('UTF-8' !== ($encoding = self::getEncoding($encoding))) { | |
| 650 | - $s = \mb_convert_encoding($s, 'UTF-8', $encoding); | |
| 682 | + if ('UTF-8' !== $encoding = self::getEncoding($encoding)) { | |
| 683 | + $s = mb_convert_encoding($s, 'UTF-8', $encoding); | |
| 651 | 684 | } |
| 652 | 685 | if (1 === \strlen($s)) { |
| 653 | 686 | return \ord($s); |
| 654 | 687 | } |
| 655 | - $code = ($s = \unpack('C*', \substr($s, 0, 4))) ? $s[1] : 0; | |
| 688 | + $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0; | |
| 656 | 689 | if (0xf0 <= $code) { |
| 657 | 690 | return ($code - 0xf0 << 18) + ($s[2] - 0x80 << 12) + ($s[3] - 0x80 << 6) + $s[4] - 0x80; |
| 658 | 691 | } |
| 659 | 692 | if (0xe0 <= $code) { |
| @@ -663,21 +696,40 @@ | ||
| 663 | 696 | return ($code - 0xc0 << 6) + $s[2] - 0x80; |
| 664 | 697 | } |
| 665 | 698 | return $code; |
| 666 | 699 | } |
| 667 | - public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null) : string | |
| 700 | + /** @return string|false */ | |
| 701 | + public static function mb_scrub(?string $string, ?string $encoding = null): string | |
| 668 | 702 | { |
| 669 | - if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], \true)) { | |
| 670 | - throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH'); | |
| 703 | + if (null === $encoding) { | |
| 704 | + $encoding = self::mb_internal_encoding(); | |
| 705 | + } elseif (!self::assertEncoding($encoding, 'mb_scrub(): Argument #2 ($encoding) must be a valid encoding, "%s" given')) { | |
| 706 | + return \false; | |
| 671 | 707 | } |
| 708 | + return self::mb_convert_encoding((string) $string, $encoding, $encoding); | |
| 709 | + } | |
| 710 | + /** @return string|false */ | |
| 711 | + public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null) | |
| 712 | + { | |
| 672 | 713 | if (null === $encoding) { |
| 673 | 714 | $encoding = self::mb_internal_encoding(); |
| 674 | - } else { | |
| 675 | - self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given'); | |
| 715 | + } elseif (!self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given')) { | |
| 716 | + return \false; | |
| 676 | 717 | } |
| 677 | 718 | if (self::mb_strlen($pad_string, $encoding) <= 0) { |
| 719 | + if (\PHP_VERSION_ID < 80000) { | |
| 720 | + trigger_error('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', \E_USER_WARNING); | |
| 721 | + return \false; | |
| 722 | + } | |
| 678 | 723 | throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string'); |
| 679 | 724 | } |
| 725 | + if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], \true)) { | |
| 726 | + if (\PHP_VERSION_ID < 80000) { | |
| 727 | + trigger_error('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', \E_USER_WARNING); | |
| 728 | + return \false; | |
| 729 | + } | |
| 730 | + throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH'); | |
| 731 | + } | |
| 680 | 732 | $paddingRequired = $length - self::mb_strlen($string, $encoding); |
| 681 | 733 | if ($paddingRequired < 1) { |
| 682 | 734 | return $string; |
| 683 | 735 | } |
| @@ -682,39 +734,56 @@ | ||
| 682 | 734 | return $string; |
| 683 | 735 | } |
| 684 | 736 | switch ($pad_type) { |
| 685 | 737 | case \STR_PAD_LEFT: |
| 686 | - return self::mb_substr(\str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding) . $string; | |
| 738 | + return self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding) . $string; | |
| 687 | 739 | case \STR_PAD_RIGHT: |
| 688 | - return $string . self::mb_substr(\str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding); | |
| 740 | + return $string . self::mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding); | |
| 689 | 741 | default: |
| 690 | - $leftPaddingLength = \floor($paddingRequired / 2); | |
| 742 | + $leftPaddingLength = floor($paddingRequired / 2); | |
| 691 | 743 | $rightPaddingLength = $paddingRequired - $leftPaddingLength; |
| 692 | - return self::mb_substr(\str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding) . $string . self::mb_substr(\str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding); | |
| 744 | + return self::mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding) . $string . self::mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding); | |
| 693 | 745 | } |
| 694 | 746 | } |
| 695 | - public static function mb_ucfirst(string $string, ?string $encoding = null) : string | |
| 747 | + /** @return string|false */ | |
| 748 | + public static function mb_ucfirst(string $string, ?string $encoding = null) | |
| 696 | 749 | { |
| 697 | 750 | if (null === $encoding) { |
| 698 | 751 | $encoding = self::mb_internal_encoding(); |
| 699 | - } else { | |
| 700 | - self::assertEncoding($encoding, 'mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given'); | |
| 752 | + } elseif (!self::assertEncoding($encoding, 'mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given')) { | |
| 753 | + return \false; | |
| 701 | 754 | } |
| 702 | - $firstChar = \mb_substr($string, 0, 1, $encoding); | |
| 703 | - $firstChar = \mb_convert_case($firstChar, \MB_CASE_TITLE, $encoding); | |
| 704 | - return $firstChar . \mb_substr($string, 1, null, $encoding); | |
| 755 | + $firstChar = mb_substr($string, 0, 1, $encoding); | |
| 756 | + $firstChar = mb_convert_case($firstChar, \MB_CASE_TITLE, $encoding); | |
| 757 | + return $firstChar . mb_substr($string, 1, null, $encoding); | |
| 705 | 758 | } |
| 706 | - public static function mb_lcfirst(string $string, ?string $encoding = null) : string | |
| 759 | + /** @return string|false */ | |
| 760 | + public static function mb_lcfirst(string $string, ?string $encoding = null) | |
| 707 | 761 | { |
| 708 | 762 | if (null === $encoding) { |
| 709 | 763 | $encoding = self::mb_internal_encoding(); |
| 710 | - } else { | |
| 711 | - self::assertEncoding($encoding, 'mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given'); | |
| 764 | + } elseif (!self::assertEncoding($encoding, 'mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given')) { | |
| 765 | + return \false; | |
| 712 | 766 | } |
| 713 | - $firstChar = \mb_substr($string, 0, 1, $encoding); | |
| 714 | - $firstChar = \mb_convert_case($firstChar, \MB_CASE_LOWER, $encoding); | |
| 715 | - return $firstChar . \mb_substr($string, 1, null, $encoding); | |
| 767 | + $firstChar = mb_substr($string, 0, 1, $encoding); | |
| 768 | + $firstChar = mb_convert_case($firstChar, \MB_CASE_LOWER, $encoding); | |
| 769 | + return $firstChar . mb_substr($string, 1, null, $encoding); | |
| 716 | 770 | } |
| 771 | + /** @return string|false */ | |
| 772 | + public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 773 | + { | |
| 774 | + return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 775 | + } | |
| 776 | + /** @return string|false */ | |
| 777 | + public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 778 | + { | |
| 779 | + return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 780 | + } | |
| 781 | + /** @return string|false */ | |
| 782 | + public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null) | |
| 783 | + { | |
| 784 | + return self::mb_internal_trim('{[%s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 785 | + } | |
| 717 | 786 | private static function getSubpart($pos, $part, $haystack, $encoding) |
| 718 | 787 | { |
| 719 | 788 | if (\false === $pos) { |
| 720 | 789 | return \false; |
| @@ -727,9 +796,9 @@ | ||
| 727 | 796 | private static function html_encoding_callback(array $m) |
| 728 | 797 | { |
| 729 | 798 | $i = 1; |
| 730 | 799 | $entities = ''; |
| 731 | - $m = \unpack('C*', \htmlentities($m[0], \ENT_COMPAT, 'UTF-8')); | |
| 800 | + $m = unpack('C*', htmlentities($m[0], \ENT_COMPAT, 'UTF-8')); | |
| 732 | 801 | while (isset($m[$i])) { |
| 733 | 802 | if (0x80 > $m[$i]) { |
| 734 | 803 | $entities .= \chr($m[$i++]); |
| 735 | 804 | continue; |
| @@ -750,9 +819,9 @@ | ||
| 750 | 819 | return self::mb_convert_case($s[1], \MB_CASE_UPPER, 'UTF-8') . self::mb_convert_case($s[2], \MB_CASE_LOWER, 'UTF-8'); |
| 751 | 820 | } |
| 752 | 821 | private static function getData($file) |
| 753 | 822 | { |
| 754 | - if (\file_exists($file = __DIR__ . '/Resources/unidata/' . $file . '.php')) { | |
| 823 | + if (file_exists($file = __DIR__ . '/Resources/unidata/' . $file . '.php')) { | |
| 755 | 824 | return require $file; |
| 756 | 825 | } |
| 757 | 826 | return \false; |
| 758 | 827 | } |
| @@ -763,9 +832,9 @@ | ||
| 763 | 832 | } |
| 764 | 833 | if ('UTF-8' === $encoding) { |
| 765 | 834 | return 'UTF-8'; |
| 766 | 835 | } |
| 767 | - $encoding = \strtoupper($encoding); | |
| 836 | + $encoding = strtoupper($encoding); | |
| 768 | 837 | if ('8BIT' === $encoding || 'BINARY' === $encoding) { |
| 769 | 838 | return 'CP850'; |
| 770 | 839 | } |
| 771 | 840 | if ('UTF8' === $encoding) { |
| @@ -770,28 +839,30 @@ | ||
| 770 | 839 | } |
| 771 | 840 | if ('UTF8' === $encoding) { |
| 772 | 841 | return 'UTF-8'; |
| 773 | 842 | } |
| 843 | + if ('UTF-32' === $encoding) { | |
| 844 | + return 'UTF-32BE'; | |
| 845 | + } | |
| 846 | + if ('UTF-16' === $encoding) { | |
| 847 | + return 'UTF-16BE'; | |
| 848 | + } | |
| 774 | 849 | return $encoding; |
| 775 | 850 | } |
| 776 | - public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) : string | |
| 851 | + private static function iconv($fromEncoding, $toEncoding, $s) | |
| 777 | 852 | { |
| 778 | - return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 853 | + if (null === self::$iconvSupportsIgnore) { | |
| 854 | + self::$iconvSupportsIgnore = \false !== @iconv('UTF-8', 'UTF-8//IGNORE', ''); | |
| 855 | + } | |
| 856 | + return self::$iconvSupportsIgnore ? iconv($fromEncoding, $toEncoding . '//IGNORE', $s) : iconv($fromEncoding, $toEncoding, $s); | |
| 779 | 857 | } |
| 780 | - public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null) : string | |
| 858 | + /** @return string|false */ | |
| 859 | + private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function) | |
| 781 | 860 | { |
| 782 | - return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__); | |
| 783 | - } | |
| 784 | - public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null) : string | |
| 785 | - { | |
| 786 | - return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__); | |
| 787 | - } | |
| 788 | - private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function) : string | |
| 789 | - { | |
| 790 | 861 | if (null === $encoding) { |
| 791 | 862 | $encoding = self::mb_internal_encoding(); |
| 792 | - } else { | |
| 793 | - self::assertEncoding($encoding, $function . '(): Argument #3 ($encoding) must be a valid encoding, "%s" given'); | |
| 863 | + } elseif (!self::assertEncoding($encoding, $function . '(): Argument #3 ($encoding) must be a valid encoding, "%s" given')) { | |
| 864 | + return \false; | |
| 794 | 865 | } |
| 795 | 866 | if ('' === $characters) { |
| 796 | 867 | return null === $encoding ? $string : self::mb_convert_encoding($string, $encoding); |
| 797 | 868 | } |
| @@ -796,32 +867,32 @@ | ||
| 796 | 867 | return null === $encoding ? $string : self::mb_convert_encoding($string, $encoding); |
| 797 | 868 | } |
| 798 | 869 | if ('UTF-8' === $encoding) { |
| 799 | 870 | $encoding = null; |
| 800 | - if (!\preg_match('//u', $string)) { | |
| 801 | - $string = @\iconv('UTF-8', 'UTF-8//IGNORE', $string); | |
| 871 | + if (!preg_match('//u', $string)) { | |
| 872 | + $string = @self::iconv('UTF-8', 'UTF-8', $string); | |
| 802 | 873 | } |
| 803 | - if (null !== $characters && !\preg_match('//u', $characters)) { | |
| 804 | - $characters = @\iconv('UTF-8', 'UTF-8//IGNORE', $characters); | |
| 874 | + if (null !== $characters && !preg_match('//u', $characters)) { | |
| 875 | + $characters = @self::iconv('UTF-8', 'UTF-8', $characters); | |
| 805 | 876 | } |
| 806 | 877 | } else { |
| 807 | - $string = \iconv($encoding, 'UTF-8//IGNORE', $string); | |
| 878 | + $string = self::iconv($encoding, 'UTF-8', $string); | |
| 808 | 879 | if (null !== $characters) { |
| 809 | - $characters = \iconv($encoding, 'UTF-8//IGNORE', $characters); | |
| 880 | + $characters = self::iconv($encoding, 'UTF-8', $characters); | |
| 810 | 881 | } |
| 811 | 882 | } |
| 812 | 883 | if (null === $characters) { |
| 813 | 884 | $characters = "\\0 \f\n\r\t\v "; |
| 814 | 885 | } else { |
| 815 | - $characters = \preg_quote($characters); | |
| 886 | + $characters = preg_quote($characters); | |
| 816 | 887 | } |
| 817 | - $string = \preg_replace(\sprintf($regex, $characters), '', $string); | |
| 888 | + $string = preg_replace(\sprintf($regex, $characters), '', $string); | |
| 818 | 889 | if (null === $encoding) { |
| 819 | 890 | return $string; |
| 820 | 891 | } |
| 821 | - return \iconv('UTF-8', $encoding . '//IGNORE', $string); | |
| 892 | + return self::iconv('UTF-8', $encoding, $string); | |
| 822 | 893 | } |
| 823 | - private static function assertEncoding(string $encoding, string $errorFormat) : void | |
| 894 | + private static function assertEncoding(string $encoding, string $errorFormat): bool | |
| 824 | 895 | { |
| 825 | 896 | try { |
| 826 | 897 | $validEncoding = @self::mb_check_encoding('', $encoding); |
| 827 | 898 | } catch (\ValueError $e) { |
| @@ -826,10 +897,14 @@ | ||
| 826 | 897 | $validEncoding = @self::mb_check_encoding('', $encoding); |
| 827 | 898 | } catch (\ValueError $e) { |
| 828 | 899 | throw new \ValueError(\sprintf($errorFormat, $encoding)); |
| 829 | 900 | } |
| 830 | - // BC for PHP 7.3 and lower | |
| 831 | 901 | if (!$validEncoding) { |
| 832 | - throw new \ValueError(\sprintf($errorFormat, $encoding)); | |
| 902 | + if (80000 > \PHP_VERSION_ID) { | |
| 903 | + trigger_error(\sprintf($errorFormat, $encoding), \E_USER_WARNING); | |
| 904 | + } else { | |
| 905 | + throw new \ValueError(\sprintf($errorFormat, $encoding)); | |
| 906 | + } | |
| 833 | 907 | } |
| 908 | + return $validEncoding; | |
| 834 | 909 | } |
| 835 | 910 | } |