PHP:mb_strtoupper无法正常工作 [英] PHP: mb_strtoupper not working

查看:182
本文介绍了PHP:mb_strtoupper无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对UTF-8和mb_strtoupper有问题.

I have a problem with UTF-8 and mb_strtoupper.

mb_internal_encoding('UTF-8');
$guesstitlestring='Le Courrier de Sáint-Hyácinthe';

$encoding=mb_detect_encoding($guesstitlestring);
if ($encoding!=='UTF-8') $guesstitlestring=mb_convert_encoding($guesstitlestring,'UTF-8',$encoding);

echo "DEBUG1 $guesstitlestring\n";
$guesstitlestring=mb_strtoupper($guesstitlestring);
echo "DEBUG2 $guesstitlestring\n";

结果:

DEBUG1 Le Courrier de Sáint-Hyácinthe
DEBUG2 LE COURRIER DE S?INT-HY?CINTHE

我不明白为什么会这样?我正在尝试尽可能谨慎地进行编码.该字符串首先以UTF-8的形式给出,经过验证并可能转换为UTF-8.这是一场噩梦!

I don't understand why this is happening? I'm trying to be as careful as I can with the encoding. The string is given first as a UTF-8, verified and possible reconverted to UTF-8. It's a nightmare!

更新

因此,我发现这是由我通过控制台输入参数和从控制台返回参数所造成的.因此,它们在进入和退出时都出现了乱码.解决方案是不要以这种方式输入任何参数,也不要以这种方式取出参数.

So I've figured out that this was caused by a combination of my entering the arguments via the console and the arguments coming back out of the console. So they were garbled both on the way in and the way out. The solution is to not enter any of the arguments in this way, or get the arguments out in this way.

感谢大家对解决此问题的帮助!

Thank you everyone for your help in resolving this issue!

推荐答案

使用mb_convert_case()代替strtoupper()/mb_strtoupper(),因为在不同编码中大写转换非常棘手,还请确保您的字符串为UTF-8.

Instead of strtoupper()/mb_strtoupper() use mb_convert_case() since upper case converting is very tricky across different encodings, also make sure your string IS UTF-8.

$content = 'Le Courrier de Sáint-Hyácinthe';

mb_internal_encoding('UTF-8');
if(!mb_check_encoding($content, 'UTF-8')
    OR !($content === mb_convert_encoding(mb_convert_encoding($content, 'UTF-32', 'UTF-8' ), 'UTF-8', 'UTF-32'))) {

    $content = mb_convert_encoding($content, 'UTF-8'); 
}

// LE COURRIER DE SÁINT-HYÁCINTHE
echo mb_convert_case($content, MB_CASE_UPPER, "UTF-8"); 

工作示例: http://3v4l.org/enEfm#v443

Working example: http://3v4l.org/enEfm#v443

另请参阅我在PHP网站上关于转换器的评论: http://www.php.net/manual/function.utf8-encode.php#102382

See also my comment at the PHP website about the converter: http://www.php.net/manual/function.utf8-encode.php#102382

这篇关于PHP:mb_strtoupper无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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