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

What is the difference between iconv() and mb_convert_encoding() in PHP? Does it work better, faster, etc. (For example, with specific encodings)? In which situations is preferable to others?

Here is what I think I know already:

Iconv ()

  • included in most PHP installations.
  • when characters are found that cannot be matched with the new character set, you can indicate whether they were converted to a "similar" character or ignored.

mb_convert_encoding ()

  • usually you need to install the php-mbstring extension.
  • able to process HTML-ENTITIES , convert to and from hexadecimal codes of websites.

Are there any other differences?

+44
php special-characters character-encoding
Nov 22 2018-11-11T00:
source share
2 answers

iconv() is just a shell around the iconv() function found in the C system library where PHP runs (if PHP is not built using GNU iconv, GNU iconv is used in this case). Thus, the performance and functions of iconv() depend on where you use PHP and how it is built.

The implementation of mb_convert_encoding() , on the other hand, is included in the source code of PHP (the module). It includes a library called libmbfl that handles the actual conversion. This way it works the same no matter where you use PHP. Here is a list of supported encodings: http://php.net/manual/en/mbstring.encodings.php

So, in general, I think you could say that mb_convert_encoding() 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 functions also depends on the particular implementation of iconv() , obviously.

+22
Aug 26 '14 at 15:53
source share

Since PHP 5.4 there is an error. Iconv once returns an empty string instead of returning a string with a "similar" char.

So you should use mb_convert_encoding.

+3
Mar 12 '16 at 10:49 on
source share



All Articles