PHP 中的 iconv() 和 mb_convert_encoding() 有什么区别?

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

What is the difference between iconv() and mb_convert_encoding() in PHP?

phpcharacter-encodingspecial-characters

提问by T. Brian Jones

What's the difference between iconv()and mb_convert_encoding()in PHP? Does one perform better, faster, etc. ( eg. with specific encodings )? In what situations would one be preferred over the other?

PHPiconv()mb_convert_encoding()PHP 中有什么区别?是否表现更好、更快等(例如,使用特定编码)?在什么情况下,一种会比另一种更受欢迎?

Here's what I think I know already:

以下是我认为我已经知道的:

iconv()

iconv()

  1. included with most installs of PHP.
  2. when characters that can't be mapped to the new character set are found, you can specify if they are converted to a 'similar' character, or ignored.
  1. 包含在大多数 PHP 安装中。
  2. 当找到无法映射到新字符集的字符时,您可以指定是将它们转换为“类似”字符还是忽略它们。

mb_convert_encoding()

mb_convert_encoding()

  1. usually requires installing the php-mbstring extension.
  2. is able to handle HTML-ENTITIES, converting to and from web hex codes.
  1. 通常需要安装 php-mbstring 扩展。
  2. 能够处理HTML-ENTITIES和转换网络十六进制代码。

Are there other differences?

还有其他区别吗?

采纳答案by pelle

iconv()is just a wrapper around the iconv()function found in the system C library where PHP is running (unless PHP is built with GNU iconv, in which case GNU iconv is used). So the performance and features of iconv()depend on where you are running PHP and how it is built.

iconv()只是iconv()在 PHP 运行的系统 C 库中找到的函数的包装器(除非 PHP 是用 GNU iconv 构建的,在这种情况下使用 GNU iconv)。所以性能和特性iconv()取决于你在哪里运行 PHP 以及它是如何构建的。

The implementation of mb_convert_encoding(), on the other hand, is included in the PHP (module) source. It includes a library called libmbfl which handles the actual conversion. Thus it works the same regardless of where you're running PHP. There is a list of supported encodings here: http://php.net/manual/en/mbstring.encodings.php

的实施mb_convert_encoding(),在另一方面,被包括在PHP(模块)源。它包含一个名为 libmbfl 的库,用于处理实际的转换。因此,无论您在何处运行 PHP,它的工作方式都是一样的。这里有一个支持的编码列表:http: //php.net/manual/en/mbstring.encodings.php

So, in summary, I guess you could say that mb_convert_encoding()is more reliable to use if you want to support different platforms. However, if you use iconv()on Linux (for example), then it supports a lot more encodings (see iconv --list).

因此,总而言之,mb_convert_encoding()如果您想支持不同的平台,我想您可以说使用起来更可靠。但是,如果您iconv()在 Linux 上使用(例如),那么它支持更多的编码(请参阅 参考资料iconv --list)。

The relative performance of the functions also depends on the specific iconv()implementation, obviously.

iconv()显然,这些功能的相对性能还取决于具体的实现。

回答by webshaker

Since PHP 5.4 there is a bug. Sometime iconv returns null string instead of returning a string with 'similar' char.

从 PHP 5.4 开始有一个错误。有时 iconv 返回空字符串而不是返回带有“相似”字符的字符串。

So you should use mb_convert_encoding.

所以你应该使用mb_convert_encoding。