| @@ -48,8 +48,11 @@ | ||
| 48 | 48 | * - mb_strwidth - Return width of string |
| 49 | 49 | * - mb_substr_count - Count the number of substring occurrences |
| 50 | 50 | * - mb_ucfirst - Make a string's first character uppercase |
| 51 | 51 | * - mb_lcfirst - Make a string's first character lowercase |
| 52 | + * - mb_trim - Strip whitespace (or other characters) from the beginning and end of a string | |
| 53 | + * - mb_ltrim - Strip whitespace (or other characters) from the beginning of a string | |
| 54 | + * - mb_rtrim - Strip whitespace (or other characters) from the end of a string | |
| 52 | 55 | * |
| 53 | 56 | * Not implemented: |
| 54 | 57 | * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more) |
| 55 | 58 | * - mb_ereg_* - Regular expression with multibyte support |
| @@ -72,15 +75,12 @@ | ||
| 72 | 75 | private const SIMPLE_CASE_FOLD = [['µ', 'ſ', "ͅ", 'ς', "ϐ", "ϑ", "ϕ", "ϖ", "ϰ", "ϱ", "ϵ", "ẛ", "ι"], ['μ', 's', 'ι', 'σ', 'β', 'θ', 'φ', 'π', 'κ', 'ρ', 'ε', "ṡ", 'ι']]; |
| 73 | 76 | private static $encodingList = ['ASCII', 'UTF-8']; |
| 74 | 77 | private static $language = 'neutral'; |
| 75 | 78 | private static $internalEncoding = 'UTF-8'; |
| 79 | + private static $iconvSupportsIgnore; | |
| 76 | 80 | public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null) |
| 77 | 81 | { |
| 78 | 82 | if (\is_array($s)) { |
| 79 | - if (\PHP_VERSION_ID < 70200) { | |
| 80 | - trigger_error('mb_convert_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING); | |
| 81 | - return null; | |
| 82 | - } | |
| 83 | 83 | $r = []; |
| 84 | 84 | foreach ($s as $str) { |
| 85 | 85 | $r[] = self::mb_convert_encoding($str, $toEncoding, $fromEncoding); |
| 86 | 86 | } |
| @@ -103,22 +103,48 @@ | ||
| 103 | 103 | if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) { |
| 104 | 104 | $fromEncoding = 'Windows-1252'; |
| 105 | 105 | } |
| 106 | 106 | if ('UTF-8' !== $fromEncoding) { |
| 107 | - $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s); | |
| 107 | + $s = self::iconv($fromEncoding, 'UTF-8', $s); | |
| 108 | 108 | } |
| 109 | 109 | return preg_replace_callback('/[\x80-\xFF]+/', [__CLASS__, 'html_encoding_callback'], $s); |
| 110 | 110 | } |
| 111 | 111 | if ('HTML-ENTITIES' === $fromEncoding) { |
| 112 | - $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 | + } | |
| 113 | 139 | $fromEncoding = 'UTF-8'; |
| 114 | 140 | } |
| 115 | - return iconv($fromEncoding, $toEncoding . '//IGNORE', $s); | |
| 141 | + return self::iconv($fromEncoding, $toEncoding, $s); | |
| 116 | 142 | } |
| 117 | 143 | public static function mb_convert_variables($toEncoding, $fromEncoding, &...$vars) |
| 118 | 144 | { |
| 119 | 145 | $ok = \true; |
| 120 | - array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) { | |
| 146 | + array_walk_recursive($vars, static function (&$v) use (&$ok, $toEncoding, $fromEncoding) { | |
| 121 | 147 | if (\false === $v = self::mb_convert_encoding($v, $toEncoding, $fromEncoding)) { |
| 122 | 148 | $ok = \false; |
| 123 | 149 | } |
| 124 | 150 | }); |
| @@ -153,12 +179,12 @@ | ||
| 153 | 179 | $encoding = self::getEncoding($encoding); |
| 154 | 180 | if ('UTF-8' === $encoding) { |
| 155 | 181 | $encoding = null; |
| 156 | 182 | if (!preg_match('//u', $s)) { |
| 157 | - $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 183 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 158 | 184 | } |
| 159 | 185 | } else { |
| 160 | - $s = iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 186 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 161 | 187 | } |
| 162 | 188 | $cnt = floor(\count($convmap) / 4) * 4; |
| 163 | 189 | for ($i = 0; $i < $cnt; $i += 4) { |
| 164 | 190 | // collector_decode_htmlnumericentity ignores $convmap[$i + 3] |
| @@ -164,9 +190,9 @@ | ||
| 164 | 190 | // collector_decode_htmlnumericentity ignores $convmap[$i + 3] |
| 165 | 191 | $convmap[$i] += $convmap[$i + 2]; |
| 166 | 192 | $convmap[$i + 1] += $convmap[$i + 2]; |
| 167 | 193 | } |
| 168 | - $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use ($cnt, $convmap) { | |
| 194 | + $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))' . (\PHP_VERSION_ID >= 80200 ? '' : '(?!&)') . ';?/', static function (array $m) use ($cnt, $convmap) { | |
| 169 | 195 | $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1]; |
| 170 | 196 | for ($i = 0; $i < $cnt; $i += 4) { |
| 171 | 197 | if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) { |
| 172 | 198 | return self::mb_chr($c - $convmap[$i + 2]); |
| @@ -176,9 +202,9 @@ | ||
| 176 | 202 | }, $s); |
| 177 | 203 | if (null === $encoding) { |
| 178 | 204 | return $s; |
| 179 | 205 | } |
| 180 | - return iconv('UTF-8', $encoding . '//IGNORE', $s); | |
| 206 | + return self::iconv('UTF-8', $encoding, $s); | |
| 181 | 207 | } |
| 182 | 208 | public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = \false) |
| 183 | 209 | { |
| 184 | 210 | if (null !== $s && !\is_scalar($s) && !(\is_object($s) && method_exists($s, '__toString'))) { |
| @@ -204,12 +230,12 @@ | ||
| 204 | 230 | $encoding = self::getEncoding($encoding); |
| 205 | 231 | if ('UTF-8' === $encoding) { |
| 206 | 232 | $encoding = null; |
| 207 | 233 | if (!preg_match('//u', $s)) { |
| 208 | - $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 234 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 209 | 235 | } |
| 210 | 236 | } else { |
| 211 | - $s = iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 237 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 212 | 238 | } |
| 213 | 239 | static $ulenMask = ["\xc0" => 2, "\xd0" => 2, "\xe0" => 3, "\xf0" => 4]; |
| 214 | 240 | $cnt = floor(\count($convmap) / 4) * 4; |
| 215 | 241 | $i = 0; |
| @@ -215,9 +241,9 @@ | ||
| 215 | 241 | $i = 0; |
| 216 | 242 | $len = \strlen($s); |
| 217 | 243 | $result = ''; |
| 218 | 244 | while ($i < $len) { |
| 219 | - $ulen = ($s[$i] < "\x80") ? 1 : $ulenMask[$s[$i] & "\xf0"]; | |
| 245 | + $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xf0"]; | |
| 220 | 246 | $uchr = substr($s, $i, $ulen); |
| 221 | 247 | $i += $ulen; |
| 222 | 248 | $c = self::mb_ord($uchr); |
| 223 | 249 | for ($j = 0; $j < $cnt; $j += 4) { |
| @@ -222,9 +248,9 @@ | ||
| 222 | 248 | $c = self::mb_ord($uchr); |
| 223 | 249 | for ($j = 0; $j < $cnt; $j += 4) { |
| 224 | 250 | if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) { |
| 225 | 251 | $cOffset = $c + $convmap[$j + 2] & $convmap[$j + 3]; |
| 226 | - $result .= $is_hex ? sprintf('&#x%X;', $cOffset) : ('&#' . $cOffset . ';'); | |
| 252 | + $result .= $is_hex ? \sprintf('&#x%X;', $cOffset) : '&#' . $cOffset . ';'; | |
| 227 | 253 | continue 2; |
| 228 | 254 | } |
| 229 | 255 | } |
| 230 | 256 | $result .= $uchr; |
| @@ -231,9 +257,9 @@ | ||
| 231 | 257 | } |
| 232 | 258 | if (null === $encoding) { |
| 233 | 259 | return $result; |
| 234 | 260 | } |
| 235 | - return iconv('UTF-8', $encoding . '//IGNORE', $result); | |
| 261 | + return self::iconv('UTF-8', $encoding, $result); | |
| 236 | 262 | } |
| 237 | 263 | public static function mb_convert_case($s, $mode, $encoding = null) |
| 238 | 264 | { |
| 239 | 265 | $s = (string) $s; |
| @@ -243,12 +269,12 @@ | ||
| 243 | 269 | $encoding = self::getEncoding($encoding); |
| 244 | 270 | if ('UTF-8' === $encoding) { |
| 245 | 271 | $encoding = null; |
| 246 | 272 | if (!preg_match('//u', $s)) { |
| 247 | - $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s); | |
| 273 | + $s = @self::iconv('UTF-8', 'UTF-8', $s); | |
| 248 | 274 | } |
| 249 | 275 | } else { |
| 250 | - $s = iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 276 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 251 | 277 | } |
| 252 | 278 | if (\MB_CASE_TITLE == $mode) { |
| 253 | 279 | static $titleRegexp = null; |
| 254 | 280 | if (null === $titleRegexp) { |
| @@ -279,9 +305,9 @@ | ||
| 279 | 305 | static $ulenMask = ["\xc0" => 2, "\xd0" => 2, "\xe0" => 3, "\xf0" => 4]; |
| 280 | 306 | $i = 0; |
| 281 | 307 | $len = \strlen($s); |
| 282 | 308 | while ($i < $len) { |
| 283 | - $ulen = ($s[$i] < "\x80") ? 1 : $ulenMask[$s[$i] & "\xf0"]; | |
| 309 | + $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xf0"]; | |
| 284 | 310 | $uchr = substr($s, $i, $ulen); |
| 285 | 311 | $i += $ulen; |
| 286 | 312 | if (isset($map[$uchr])) { |
| 287 | 313 | $uchr = $map[$uchr]; |
| @@ -301,9 +327,9 @@ | ||
| 301 | 327 | } |
| 302 | 328 | if (null === $encoding) { |
| 303 | 329 | return $s; |
| 304 | 330 | } |
| 305 | - return iconv('UTF-8', $encoding . '//IGNORE', $s); | |
| 331 | + return self::iconv('UTF-8', $encoding, $s); | |
| 306 | 332 | } |
| 307 | 333 | public static function mb_internal_encoding($encoding = null) |
| 308 | 334 | { |
| 309 | 335 | if (null === $encoding) { |
| @@ -316,9 +342,9 @@ | ||
| 316 | 342 | } |
| 317 | 343 | if (80000 > \PHP_VERSION_ID) { |
| 318 | 344 | return \false; |
| 319 | 345 | } |
| 320 | - throw new \ValueError(sprintf('Argument #1 ($encoding) must be a valid encoding, "%s" given', $encoding)); | |
| 346 | + throw new \ValueError(\sprintf('Argument #1 ($encoding) must be a valid encoding, "%s" given', $encoding)); | |
| 321 | 347 | } |
| 322 | 348 | public static function mb_language($lang = null) |
| 323 | 349 | { |
| 324 | 350 | if (null === $lang) { |
| @@ -332,9 +358,9 @@ | ||
| 332 | 358 | } |
| 333 | 359 | if (80000 > \PHP_VERSION_ID) { |
| 334 | 360 | return \false; |
| 335 | 361 | } |
| 336 | - throw new \ValueError(sprintf('Argument #1 ($language) must be a valid language, "%s" given', $lang)); | |
| 362 | + throw new \ValueError(\sprintf('Argument #1 ($language) must be a valid language, "%s" given', $lang)); | |
| 337 | 363 | } |
| 338 | 364 | public static function mb_list_encodings() |
| 339 | 365 | { |
| 340 | 366 | return ['UTF-8']; |
| @@ -349,12 +375,8 @@ | ||
| 349 | 375 | return \false; |
| 350 | 376 | } |
| 351 | 377 | public static function mb_check_encoding($var = null, $encoding = null) |
| 352 | 378 | { |
| 353 | - if (\PHP_VERSION_ID < 70200 && \is_array($var)) { | |
| 354 | - trigger_error('mb_check_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING); | |
| 355 | - return null; | |
| 356 | - } | |
| 357 | 379 | if (null === $encoding) { |
| 358 | 380 | if (null === $var) { |
| 359 | 381 | return \false; |
| 360 | 382 | } |
| @@ -433,9 +455,15 @@ | ||
| 433 | 455 | $encoding = self::getEncoding($encoding); |
| 434 | 456 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 435 | 457 | return \strlen($s); |
| 436 | 458 | } |
| 437 | - 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); | |
| 438 | 466 | } |
| 439 | 467 | public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null) |
| 440 | 468 | { |
| 441 | 469 | $encoding = self::getEncoding($encoding); |
| @@ -469,10 +497,10 @@ | ||
| 469 | 497 | } else { |
| 470 | 498 | $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding); |
| 471 | 499 | } |
| 472 | 500 | } |
| 473 | - $pos = ('' !== $needle || 80000 > \PHP_VERSION_ID) ? iconv_strrpos($haystack, $needle, $encoding) : self::mb_strlen($haystack, $encoding); | |
| 474 | - return (\false !== $pos) ? $offset + $pos : \false; | |
| 501 | + $pos = '' !== $needle || 80000 > \PHP_VERSION_ID ? iconv_strrpos($haystack, $needle, $encoding) : self::mb_strlen($haystack, $encoding); | |
| 502 | + return \false !== $pos ? $offset + $pos : \false; | |
| 475 | 503 | } |
| 476 | 504 | public static function mb_str_split($string, $split_length = 1, $encoding = null) |
| 477 | 505 | { |
| 478 | 506 | if (null !== $string && !\is_scalar($string) && !(\is_object($string) && method_exists($string, '__toString'))) { |
| @@ -532,9 +560,9 @@ | ||
| 532 | 560 | public static function mb_substr($s, $start, $length = null, $encoding = null) |
| 533 | 561 | { |
| 534 | 562 | $encoding = self::getEncoding($encoding); |
| 535 | 563 | if ('CP850' === $encoding || 'ASCII' === $encoding) { |
| 536 | - return (string) substr($s, $start, (null === $length) ? 2147483647 : $length); | |
| 564 | + return (string) substr($s, $start, null === $length ? 2147483647 : $length); | |
| 537 | 565 | } |
| 538 | 566 | if ($start < 0) { |
| 539 | 567 | $start = iconv_strlen($s, $encoding) + $start; |
| 540 | 568 | if ($start < 0) { |
| @@ -613,15 +641,15 @@ | ||
| 613 | 641 | return \false; |
| 614 | 642 | } |
| 615 | 643 | public static function mb_http_output($encoding = null) |
| 616 | 644 | { |
| 617 | - return (null !== $encoding) ? 'pass' === $encoding : 'pass'; | |
| 645 | + return null !== $encoding ? 'pass' === $encoding : 'pass'; | |
| 618 | 646 | } |
| 619 | 647 | public static function mb_strwidth($s, $encoding = null) |
| 620 | 648 | { |
| 621 | 649 | $encoding = self::getEncoding($encoding); |
| 622 | 650 | if ('UTF-8' !== $encoding) { |
| 623 | - $s = iconv($encoding, 'UTF-8//IGNORE', $s); | |
| 651 | + $s = self::iconv($encoding, 'UTF-8', $s); | |
| 624 | 652 | } |
| 625 | 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); |
| 626 | 654 | return ($wide << 1) + iconv_strlen($s, 'UTF-8'); |
| 627 | 655 | } |
| @@ -668,21 +696,40 @@ | ||
| 668 | 696 | return ($code - 0xc0 << 6) + $s[2] - 0x80; |
| 669 | 697 | } |
| 670 | 698 | return $code; |
| 671 | 699 | } |
| 672 | - 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 | |
| 673 | 702 | { |
| 674 | - if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], \true)) { | |
| 675 | - 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; | |
| 676 | 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 | + { | |
| 677 | 713 | if (null === $encoding) { |
| 678 | 714 | $encoding = self::mb_internal_encoding(); |
| 679 | - } else { | |
| 680 | - 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; | |
| 681 | 717 | } |
| 682 | 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 | + } | |
| 683 | 723 | throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string'); |
| 684 | 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 | + } | |
| 685 | 732 | $paddingRequired = $length - self::mb_strlen($string, $encoding); |
| 686 | 733 | if ($paddingRequired < 1) { |
| 687 | 734 | return $string; |
| 688 | 735 | } |
| @@ -696,30 +743,47 @@ | ||
| 696 | 743 | $rightPaddingLength = $paddingRequired - $leftPaddingLength; |
| 697 | 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); |
| 698 | 745 | } |
| 699 | 746 | } |
| 700 | - 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) | |
| 701 | 749 | { |
| 702 | 750 | if (null === $encoding) { |
| 703 | 751 | $encoding = self::mb_internal_encoding(); |
| 704 | - } else { | |
| 705 | - 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; | |
| 706 | 754 | } |
| 707 | 755 | $firstChar = mb_substr($string, 0, 1, $encoding); |
| 708 | 756 | $firstChar = mb_convert_case($firstChar, \MB_CASE_TITLE, $encoding); |
| 709 | 757 | return $firstChar . mb_substr($string, 1, null, $encoding); |
| 710 | 758 | } |
| 711 | - 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) | |
| 712 | 761 | { |
| 713 | 762 | if (null === $encoding) { |
| 714 | 763 | $encoding = self::mb_internal_encoding(); |
| 715 | - } else { | |
| 716 | - 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; | |
| 717 | 766 | } |
| 718 | 767 | $firstChar = mb_substr($string, 0, 1, $encoding); |
| 719 | 768 | $firstChar = mb_convert_case($firstChar, \MB_CASE_LOWER, $encoding); |
| 720 | 769 | return $firstChar . mb_substr($string, 1, null, $encoding); |
| 721 | 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 | + } | |
| 722 | 786 | private static function getSubpart($pos, $part, $haystack, $encoding) |
| 723 | 787 | { |
| 724 | 788 | if (\false === $pos) { |
| 725 | 789 | return \false; |
| @@ -775,19 +839,72 @@ | ||
| 775 | 839 | } |
| 776 | 840 | if ('UTF8' === $encoding) { |
| 777 | 841 | return 'UTF-8'; |
| 778 | 842 | } |
| 843 | + if ('UTF-32' === $encoding) { | |
| 844 | + return 'UTF-32BE'; | |
| 845 | + } | |
| 846 | + if ('UTF-16' === $encoding) { | |
| 847 | + return 'UTF-16BE'; | |
| 848 | + } | |
| 779 | 849 | return $encoding; |
| 780 | 850 | } |
| 781 | - private static function assertEncoding(string $encoding, string $errorFormat): void | |
| 851 | + private static function iconv($fromEncoding, $toEncoding, $s) | |
| 782 | 852 | { |
| 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); | |
| 857 | + } | |
| 858 | + /** @return string|false */ | |
| 859 | + private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function) | |
| 860 | + { | |
| 861 | + if (null === $encoding) { | |
| 862 | + $encoding = self::mb_internal_encoding(); | |
| 863 | + } elseif (!self::assertEncoding($encoding, $function . '(): Argument #3 ($encoding) must be a valid encoding, "%s" given')) { | |
| 864 | + return \false; | |
| 865 | + } | |
| 866 | + if ('' === $characters) { | |
| 867 | + return null === $encoding ? $string : self::mb_convert_encoding($string, $encoding); | |
| 868 | + } | |
| 869 | + if ('UTF-8' === $encoding) { | |
| 870 | + $encoding = null; | |
| 871 | + if (!preg_match('//u', $string)) { | |
| 872 | + $string = @self::iconv('UTF-8', 'UTF-8', $string); | |
| 873 | + } | |
| 874 | + if (null !== $characters && !preg_match('//u', $characters)) { | |
| 875 | + $characters = @self::iconv('UTF-8', 'UTF-8', $characters); | |
| 876 | + } | |
| 877 | + } else { | |
| 878 | + $string = self::iconv($encoding, 'UTF-8', $string); | |
| 879 | + if (null !== $characters) { | |
| 880 | + $characters = self::iconv($encoding, 'UTF-8', $characters); | |
| 881 | + } | |
| 882 | + } | |
| 883 | + if (null === $characters) { | |
| 884 | + $characters = "\\0 \f\n\r\t\v "; | |
| 885 | + } else { | |
| 886 | + $characters = preg_quote($characters); | |
| 887 | + } | |
| 888 | + $string = preg_replace(\sprintf($regex, $characters), '', $string); | |
| 889 | + if (null === $encoding) { | |
| 890 | + return $string; | |
| 891 | + } | |
| 892 | + return self::iconv('UTF-8', $encoding, $string); | |
| 893 | + } | |
| 894 | + private static function assertEncoding(string $encoding, string $errorFormat): bool | |
| 895 | + { | |
| 783 | 896 | try { |
| 784 | 897 | $validEncoding = @self::mb_check_encoding('', $encoding); |
| 785 | 898 | } catch (\ValueError $e) { |
| 786 | 899 | throw new \ValueError(\sprintf($errorFormat, $encoding)); |
| 787 | 900 | } |
| 788 | - // BC for PHP 7.3 and lower | |
| 789 | 901 | if (!$validEncoding) { |
| 790 | - 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 | + } | |
| 791 | 907 | } |
| 908 | + return $validEncoding; | |
| 792 | 909 | } |
| 793 | 910 | } |