php strtolower 函数的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2516448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:46:56  来源:igfitidea点击:

problems with strtolower function

php

提问by Simon

i have some text in foreign language in my page, but when i make it lowercase, it starts to look like this...

我的页面中有一些外语文本,但是当我将其设为小写时,它开始看起来像这样......



$a = "????? ?????????";
echo $b = strtolower($a);
//returns  ????? ?????????

i've set <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />could you tell me why? thanks in advance

我已经设置了<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />你能告诉我为什么吗?提前致谢

回答by SteelBytes

have your tried using mb_strtolower()?

您是否尝试过使用 mb_strtolower()?

回答by reko_t

PHP5 is not UTF-8 compatible, so you still need to resort to the mb extension. I suggest you set the internal encoding of mb to utf-8 and then you can freely use its functions without specifying the charset all the time:

PHP5 不兼容 UTF-8,因此您仍然需要求助于 mb 扩展名。我建议你将mb的内部编码设置为utf-8,然后你就可以自由使用它的功能,而无需一直指定字符集:

mb_internal_encoding('UTF-8');

...

$b = mb_strtolower($a);
echo $b;

回答by khaled_webdev

i have found this solution from here

我从这里找到了这个解决方案

$string = '?';
echo 'Uppercase: '.mb_convert_case($string, MB_CASE_UPPER, "UTF-8").'';
echo 'Lowercase: '.mb_convert_case($string, MB_CASE_LOWER, "UTF-8").'';
echo 'Original: '.$string.'';

works for me (lower case)

对我有用(小写)

回答by Kevin

Have you tried

你有没有尝试过

http://www.php.net/manual/en/function.mb-strtolower.php

http://www.php.net/manual/en/function.mb-strtolower.php

mb_strtolower() and specifying the encoding as the second parameter?

mb_strtolower() 并将编码指定为第二个参数?

The examples on that page appear to work.

该页面上的示例似乎有效。

You could also try:

你也可以试试:

$str = mb_strtolower($str, mb_detect_encoding($str));

回答by SWilk

Php by default does not know about utf-8. It assumes any string is ASCII, so it strtolower converts bytes containing codes of uppercase letters A-Z to codes of lowercase a-z. As the UTF-8 non-ascii letters are written with two or more bytes, the strtolower converts each byte separately, and if the byte happens to contain code equal to letters A-Z, it is converted. In the result the sequence is broken, and it no longer represents correct character.

PHP 默认不知道 utf-8。它假定任何字符串都是 ASCII,因此 strtolower 将包含大写字母 AZ 代码的字节转换为小写 az 代码。由于 UTF-8 非 ascii 字母是用两个或更多字节写入的,所以 strtolower 会分别转换每个字节,如果该字节恰好包含等于字母 AZ 的代码,则对其进行转换。结果序列被破坏,不再代表正确的字符。

To change this you need to configure the mbstring extension:

要更改此设置,您需要配置 mbstring 扩展名:

http://www.php.net/manual/en/book.mbstring.php

http://www.php.net/manual/en/book.mbstring.php

to replace strtolower with mb_strtolower or use mb_strtolower direclty. I any case, you need to spend some time to configure the mbstring settings to match your requirements.

用 mb_strtolower 替换 strtolower 或使用 mb_strtolower direclty。无论如何,您需要花一些时间来配置 mbstring 设置以满足您的要求。

回答by Powerlord

Use mb_strtolowerinstead, as strtolower doesn't work on multi-byte characters.

请改用mb_strtolower,因为 strtolower 不适用于多字节字符。

回答by Pekka

strtolower() will perform the conversion in the currently selected locale only.

strtolower() 将仅在当前选定的语言环境中执行转换。

I would try mb_convert_case(). Make sure you explicitly specify an encoding.

我会尝试mb_convert_case()。确保您明确指定编码。

回答by intuited

You will need to set the locale; see the first example at http://ca3.php.net/manual/en/function.strtolower.php

您需要设置语言环境;请参阅http://ca3.php.net/manual/en/function.strtolower.php 上的第一个示例