PHP 中的 iconv() 和 mb_convert_encoding() 有什么区别?
iconv()
和 mb_convert_encoding()
在 PHP 中有什么区别?性能是否更好、更快等(例如,使用特定编码)?在什么情况下,一种会比另一种更受欢迎?
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?
以下是我认为我已经知道的:
Here's what I think I know already:
iconv()
- 包含在大多数 PHP 安装中.
- 当找到无法映射到新字符集的字符时,您可以指定是将它们转换为类似"字符还是忽略它们.
mb_convert_encoding()
- 通常需要安装 php-mbstring 扩展.
- 能够处理
HTML-ENTITIES
,在网络十六进制代码之间进行转换.
- usually requires installing the php-mbstring extension.
- is able to handle
HTML-ENTITIES
, converting to and from web hex codes.
还有其他区别吗?
推荐答案
iconv()
只是对系统 C 库中的 iconv()
函数的封装PHP 运行的地方(除非 PHP 是用 GNU iconv 构建的,在这种情况下使用 GNU iconv).因此,iconv()
的性能和特性取决于您运行 PHP 的位置以及它的构建方式.
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.
另一方面,mb_convert_encoding()
的实现包含在 PHP(模块)源代码中.它包含一个名为 libmbfl 的库,用于处理实际的转换.因此,无论您在何处运行 PHP,它的工作方式都是一样的.这里有一个支持的编码列表:http://php.net/manual/en/mbstring.encodings.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()
使用起来更可靠.但是,如果您在 Linux 上使用 iconv()
(例如),那么它支持更多的编码(请参阅 iconv --list
).
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
).
显然,函数的相对性能还取决于特定的iconv()
实现.
The relative performance of the functions also depends on the specific iconv()
implementation, obviously.
相关文章