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()
mb_convert_encoding()
HTML-ENTITIES
, converting to and from web hex codes.Are there other differences?
Iconv converts character encoding by setting a unique number for every character. The module contains an interface to iconv character set conversion facility. We use this module to transfer the character set of a string from a local character set to another character set.
The iconv() function converts a sequence of characters in one character encoding to a sequence of characters in another character encoding.
Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.
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.
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
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
).
The relative performance of the functions also depends on the specific iconv()
implementation, obviously.
Since PHP 5.4 there is a bug. Sometime iconv returns null string instead of returning a string with 'similar' char.
So you should use mb_convert_encoding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With