php mb_strtolower 和 utf8 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13288785/
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 05:06:44  来源:igfitidea点击:

mb_strtolower and utf8 strings

phparrays

提问by behz4d

As you know, we need to use mb_strtolower()instead of strtolower()while we're working with utf-8 data:

如您所知,我们需要在处理 utf-8 数据时使用mb_strtolower()而不是strtolower()

$str = '??????';
echo strtolower($str);
----------------------
output: ?????

It's all gone to undefined chars, now I use mb_strtolower()

一切都变成了未定义的字符,现在我使用 mb_strtolower()

$str = '??????';
echo mb_strtolower($str);
----------------------
output: ?????

still the same results, now:

结果还是一样,现在:

$str = '??????';
echo mb_strtolower($str,  mb_detect_encoding($str));
----------------------
output: ??????

Now it's fixed, so the way to use mb_strtoloweris to also having mb_detect_encoding.

现在它是固定的,所以使用的方法mb_strtolower是也有mb_detect_encoding.

Now my problem is that I want to do the same thing with array_map:

现在我的问题是我想做同样的事情array_map

$results_array = array_map('mb_strtolower', $results_array);

How I'm supposed to use mb_detect_encodingwith the above line?

我应该如何mb_detect_encoding与上述行一起使用?

回答by deceze

The solution is to tell mb_strtolowerwhat your string encoding is:

解决方案是告诉mb_strtolower你的字符串编码是什么:

echo mb_strtolower($str, 'UTF-8');

If you don't want to supply this parameter every time, set it once for all mb_functions:

如果您不想每次都提供此参数,请为所有mb_功能设置一次:

mb_internal_encoding('UTF-8');

Then you can call any mb_function and it will handle your string as UTF-8:

然后你可以调用任何mb_函数,它会将你的字符串作为 UTF-8 处理:

echo mb_strtolower($str); // works without second parameter now

mb_detect_encodinghappens to return 'UTF-8'because it detected it, but it is generally unreliable, since it's conceptually impossible to reliably detect arbitrarily encoded strings. Knowwhat your strings are encoded in and pass this information explicitly.

mb_detect_encoding碰巧返回,'UTF-8'因为它检测到它,但它通常是不可靠的,因为从概念上讲不可能可靠地检测任意编码的字符串。知道你的字符串是用什么编码的,并明确地传递这些信息。

回答by Berry Langerak

Simply put, define your own function which then calls mb_strtolowerwith mb_detect_encoding.

简单地说,定义你自己的函数,然后mb_strtolowermb_detect_encoding.

$results_array = array_map(function($var) {
      return mb_strtolower($var, mb_detect_encoding($var));
}, $results_array);