php mb_convert_encoding 问题

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

mb_convert_encoding issues

phpencodingutf-8

提问by brasimon

I have some issues with the PHP function mb_detect_encoding. I can't convert it to ISO-8859-1. Any help?

我对 PHP 函数 mb_detect_encoding 有一些问题。我无法将其转换为 ISO-8859-1。有什么帮助吗?

Code:

代码:

$str = "???";
$encoding = mb_detect_encoding($str);
echo $encoding;

$encoding = mb_detect_encoding(mb_convert_encoding($str, "ISO-8859-1"));
echo $encoding;

Output:

输出:

UTF-8

UTF-8

UTF-8

UTF-8

Updated, solution:

更新,解决方案:

I updated mb_detect_order to array('UTF-8', 'ISO-8859-1') and it worked.

我将 mb_detect_order 更新为 array('UTF-8', 'ISO-8859-1') 并且它起作用了。

回答by borrible

You've not actually converted your string. Rather, the call to mb_convert_encodingdid not assume that the original string was in UTF-8. The string before the call was a byte sequence that could have been ISO-8859-1 already (and would have represented items differently). You can see this is the case by, rather than calling the mb_detect_encoding, using bin2hexon the string and seeing the byte-sequence after the conversion call. You'll see the byte sequence was unchanged.

你实际上并没有转换你的字符串。相反,对 的调用mb_convert_encoding并不假设原始字符串是 UTF-8。调用之前的字符串是一个字节序列,它可能已经是 ISO-8859-1(并且会以不同的方式表示项目)。您可以mb_detect_encoding通过bin2hex在字符串上使用,而不是调用,并在转换调用后查看字节序列来看到这种情况。你会看到字节序列没有改变。

To get the conversion to work, you need to tell it (in this case) the original encoding. Use:

要使转换工作,您需要告诉它(在本例中)原始编码。用:

mb_convert_encoding($str, 'ISO-8859-1','utf-8');

If you examine the byte-sequence after this you'll see conversion has taken place.

如果您在此之后检查字节序列,您将看到转换已经发生。

回答by brasimon

I updated mb_detect_order to array('UTF-8', 'ISO-8859-1') and it worked

我将 mb_detect_order 更新为 array('UTF-8', 'ISO-8859-1') 并且它起作用了