PHP 8.4: Mbstring: 新增 mb_trim、mb_ltrim 和 mb_rtrim 函数
PHP 8.4 为现有的 trim
、ltrim
和 rtrim
函数添加了等效的 mb_
函数。
trim
/ltrim
/rtrim
函数在字符串的开头和结尾都去掉空白字符。默认情况下,这会去掉空空格(
)、制表符(\t
)、LF(\n
)、CR(\r
)、NUL字节(\0
)和垂直制表符(\v)
字符。
新的 mb_trim
、mb_ltrim
和 mb_rtrim
函数支持多字节字符串,也支持修剪任何多字节字符。修剪的空白字符的默认列表也会更新,以包括 Unicode Z
块和其他一些通常被修剪的字符。需要修剪的字符列表可以指定为可选参数。
trim()
函数及其变体支持使用…
标记定义字符范围。比如,trim('testABC', 'A…E')
相当于trim('testABC', 'ABCDE')
。这是mb_trim()
及其变体所不支持的。
函数摘要
mb_trim
函数
多字节安全地从字符串的开头和结尾去除空白(或其他字符)。
/**
* Multi-byte safely strip white-spaces (or other characters) from the beginning and end of a string.
*
* @param string $string The string that will be trimmed.
* @param string $characters Optionally, the stripped characters can also be specified using the $characters parameter. Simply list all characters that you want to be stripped.
* @param string|null $encoding The encoding parameter is the character encoding.
*
* @return string The trimmed string.
*/
function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\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}", ?string $encoding = null): string {}
mb_ltrim
多字节安全地从字符串的开头去除空白(或其他字符)。
/**
* Multi-byte safely strip white-spaces (or other characters) from the beginning of a string.
*
* @param string $string The string that will be trimmed.
* @param string $characters Optionally, the stripped characters can also be specified using the $characters parameter. Simply list all characters that you want to be stripped.
* @param string|null $encoding The encoding parameter is the character encoding.
*
* @return string The trimmed string.
*/
function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\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}", ?string $encoding = null): string {}
mb_rtrim
多字节安全地从字符串的结尾去除空白(或其他字符)。
/**
* Multi-byte safely strip white-spaces (or other characters) from the end of a string.
*
* @param string $string The string that will be trimmed.
* @param string $characters Optionally, the stripped characters can also be specified using the $characters parameter. Simply list all characters that you want to be stripped.
* @param string|null $encoding The encoding parameter is the character encoding.
*
* @return string The trimmed string.
*/
function mb_rtrim(string $string, string $characters = " \f\n\r\t\v\x00\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}", ?string $encoding = null): string {
默认去除的字符
下表显示了默认情况下 trim()
(包括 ltrim()
和 rtrim()
) 和 mb_trim()
函数(包括 mb_ltrim()
和 mb_rtrim()
) 去除的字符。
Name | Character(s)/Regexp | Unicode/Regex representation | Removed by trim() | Removed by mb_trim |
---|---|---|---|---|
Space | \u{0020} | ✔ | ✔ | |
Tab | \t | \u{0009} | ✔ | ✔ |
End of Line | \n | \u{0009} | ✔ | ✔ |
Line Tabulation | \v | \u{000B} | ✔ | ✔ |
Carriage Return | \r | \u{000D} | ✔ | ✔ |
Form Feed | \f | \u{000D} | ✘ | ✔ |
Unicode Space Separator | \p{Z} | ✘ | ✔ | |
Null bytes | \0 | ✘ | ✔ | |
Next Line | \u{0085} | ✘ | ✔ | |
Mongolian Vowel Separator | \u{180E} | ✘ | ✔ |
用户空间 PHP Polyfill
可以使用带有正则表达式的用户端 PHP 来模拟新的 mb_trim
、mb_ltrim
和 mb_rtrim
函数的功能。
请参阅 PHPWatch/mb_trim-polyfill
,以获取为旧PHP版本提供这些新函数的完整 polyfill。此 polyfill 可以通过 composer 安装:
composer require phpwatch/mb-trim-polyfill
向后兼容性影响
mb_trim
、mb_ltrim
和 mb_rtrim
是在全局名称空间中声明的新函数。除非存在具有相同名称的函数,否则此修改不会造成任何向后兼容性影响。
可以在用户端 PHP 代码中实现这些功能。