|
50 | 50 | * - mb_substr_count - Count the number of substring occurrences |
51 | 51 | * - mb_ucfirst - Make a string's first character uppercase |
52 | 52 | * - mb_lcfirst - Make a string's first character lowercase |
| 53 | + * - mb_trim - Strip whitespace (or other characters) from the beginning and end of a string |
| 54 | + * - mb_ltrim - Strip whitespace (or other characters) from the beginning of a string |
| 55 | + * - mb_rtrim - Strip whitespace (or other characters) from the end of a string |
53 | 56 | * |
54 | 57 | * Not implemented: |
55 | 58 | * - mb_convert_kana - Convert "kana" one from another ("zen-kaku", "han-kaku" and more) |
@@ -83,7 +86,7 @@ final class Mbstring |
83 | 86 | public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null) |
84 | 87 | { |
85 | 88 | if (\is_array($s)) { |
86 | | - if (PHP_VERSION_ID < 70200) { |
| 89 | + if (\PHP_VERSION_ID < 70200) { |
87 | 90 | trigger_error('mb_convert_encoding() expects parameter 1 to be string, array given', \E_USER_WARNING); |
88 | 91 |
|
89 | 92 | return null; |
@@ -980,17 +983,75 @@ private static function getEncoding($encoding) |
980 | 983 | return $encoding; |
981 | 984 | } |
982 | 985 |
|
| 986 | + public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string |
| 987 | + { |
| 988 | + return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); |
| 989 | + } |
| 990 | + |
| 991 | + public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string |
| 992 | + { |
| 993 | + return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__); |
| 994 | + } |
| 995 | + |
| 996 | + public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string |
| 997 | + { |
| 998 | + return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__); |
| 999 | + } |
| 1000 | + |
| 1001 | + private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function): string |
| 1002 | + { |
| 1003 | + if (null === $encoding) { |
| 1004 | + $encoding = self::mb_internal_encoding(); |
| 1005 | + } else { |
| 1006 | + self::assertEncoding($encoding, $function.'(): Argument #3 ($encoding) must be a valid encoding, "%s" given'); |
| 1007 | + } |
| 1008 | + |
| 1009 | + if ('' === $characters) { |
| 1010 | + return null === $encoding ? $string : self::mb_convert_encoding($string, $encoding); |
| 1011 | + } |
| 1012 | + |
| 1013 | + if ('UTF-8' === $encoding) { |
| 1014 | + $encoding = null; |
| 1015 | + if (!preg_match('//u', $string)) { |
| 1016 | + $string = @iconv('UTF-8', 'UTF-8//IGNORE', $string); |
| 1017 | + } |
| 1018 | + if (null !== $characters && !preg_match('//u', $characters)) { |
| 1019 | + $characters = @iconv('UTF-8', 'UTF-8//IGNORE', $characters); |
| 1020 | + } |
| 1021 | + } else { |
| 1022 | + $string = iconv($encoding, 'UTF-8//IGNORE', $string); |
| 1023 | + |
| 1024 | + if (null !== $characters) { |
| 1025 | + $characters = iconv($encoding, 'UTF-8//IGNORE', $characters); |
| 1026 | + } |
| 1027 | + } |
| 1028 | + |
| 1029 | + if (null === $characters) { |
| 1030 | + $characters = "\\0 \f\n\r\t\v\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}"; |
| 1031 | + } else { |
| 1032 | + $characters = preg_quote($characters); |
| 1033 | + } |
| 1034 | + |
| 1035 | + $string = preg_replace(sprintf($regex, $characters), '', $string); |
| 1036 | + |
| 1037 | + if (null === $encoding) { |
| 1038 | + return $string; |
| 1039 | + } |
| 1040 | + |
| 1041 | + return iconv('UTF-8', $encoding.'//IGNORE', $string); |
| 1042 | + } |
| 1043 | + |
983 | 1044 | private static function assertEncoding(string $encoding, string $errorFormat): void |
984 | 1045 | { |
985 | 1046 | try { |
986 | 1047 | $validEncoding = @self::mb_check_encoding('', $encoding); |
987 | 1048 | } catch (\ValueError $e) { |
988 | | - throw new \ValueError(\sprintf($errorFormat, $encoding)); |
| 1049 | + throw new \ValueError(sprintf($errorFormat, $encoding)); |
989 | 1050 | } |
990 | 1051 |
|
991 | 1052 | // BC for PHP 7.3 and lower |
992 | 1053 | if (!$validEncoding) { |
993 | | - throw new \ValueError(\sprintf($errorFormat, $encoding)); |
| 1054 | + throw new \ValueError(sprintf($errorFormat, $encoding)); |
994 | 1055 | } |
995 | 1056 | } |
996 | 1057 | } |
0 commit comments