函数名:mb_strtolower()
适用版本:PHP 4 >= 4.3.0,PHP 5,PHP 7
用法:mb_strtolower(string $str, string|null $encoding = null): string
函数描述:mb_strtolower() 函数将字符串中的所有字符转换为小写。这个函数与 strtolower() 函数的区别在于,它可以正确地处理非 ASCII 字符和多字节字符。
参数:
- $str(必需):要转换为小写的字符串。
- $encoding(可选):要使用的字符编码。如果未指定,则使用内部字符编码。
返回值:返回转换为小写的字符串。
示例:
$str = "HELLO WORLD!";
$lowercase = mb_strtolower($str);
echo $lowercase; // 输出:hello world!
$str = "你好,世界!";
$lowercase = mb_strtolower($str, "UTF-8");
echo $lowercase; // 输出:你好,世界!(由于中文字符没有大小写之分,所以不会改变)
在上面的示例中,我们首先使用 mb_strtolower() 函数将字符串 "HELLO WORLD!" 转换为小写,并将结果存储在变量 $lowercase 中。然后,我们使用 echo 语句将结果输出到屏幕上,输出为 "hello world!"。
第二个示例中,我们使用 mb_strtolower() 函数将包含中文字符的字符串 "你好,世界!" 转换为小写。由于中文字符没有大小写之分,所以结果与原始字符串相同。请注意,我们还指定了字符编码为 "UTF-8",以确保正确处理多字节字符。