mb_strtolower和utf8字符串 [英] mb_strtolower and utf8 strings

查看:120
本文介绍了mb_strtolower和utf8字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

$str = 'برنامه';
echo strtolower($str);
----------------------
output: �����

所有这些都变成了未定义的字符,现在我使用mb_strtolower()

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

$str = 'برنامه';
echo mb_strtolower($str);
----------------------
output: �����

结果仍然相同,现在:

$str = 'برنامه';
echo mb_strtolower($str,  mb_detect_encoding($str));
----------------------
output: برنامه

现在它已固定,因此使用mb_strtolower的方法是也具有mb_detect_encoding.

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

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

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

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

我应该如何在上一行中使用mb_detect_encoding?

How I'm supposed to use mb_detect_encoding with the above line?

推荐答案

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

The solution is to tell mb_strtolower what your string encoding is:

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

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

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

mb_internal_encoding('UTF-8');

然后您可以调用任何mb_函数,它将以UTF-8的形式处理您的字符串:

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

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

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

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

这篇关于mb_strtolower和utf8字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆