76

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()

  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.

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.

Are there other differences?

Josh
  • 7,232
  • 8
  • 48
  • 75
T. Brian Jones
  • 13,002
  • 25
  • 78
  • 117
  • Related: http://stackoverflow.com/questions/4050754/iconv-or-mbstring – Maxime Pacary Nov 22 '11 at 21:06
  • 1
    It can not be said if and when any of those functions will present a problem to you because you didn't share into which *concrete* problems you run. As with any kind of function, it only does a certain job. So you will always run into problems if you use the wrong function for the job. – hakre Oct 28 '12 at 14:27
  • 18
    This is an on-topic & useful question. The OP did not ask about his specific situation and then fail to provide details to determine the preferred function in his situation; rather, he asked for information on the difference between two PHP functions which appear to perform the same function. Which, of course, begs the question: why do they both exist, and in what situations one would be preferred over the other? This is specific in scope, and broad in applicability (anyone doing character conversions in PHP would want to know this). Question should not have been closed as "not constructive." – Josh Jul 17 '14 at 21:46
  • 2
    `var_dump(similar_text('iconv','mb_convert_encoding'))` says `4`. So the answer is `4`. – Niet the Dark Absol Jul 17 '14 at 21:54
  • 4
    I agree with Josh... I came here via a Google search, looking for exactly the same information only to be disappointed that the question was rejected. I see nothing wrong with it. – John Rix Aug 11 '14 at 22:24
  • @JohnRix, What do you mean by "the question was rejected"? – Pacerier Apr 16 '15 at 05:29
  • @Pacerier, looks like the "closed as not constructive" flag has been removed from the question since. – John Rix Apr 16 '15 at 08:06
  • [Using mbstring for HTML is deprecated.](https://php.watch/versions/8.2/mbstring-qprint-base64-uuencode-html-entities-deprecated) – mbomb007 Jan 12 '23 at 14:59

2 Answers2

43

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.

pelle
  • 1,153
  • 11
  • 8
  • 8
    [iconv](http://php.net/manual/en/function.iconv.php) have `//TRANSLIT` and `//IGNORE` options to deal with non-convertable characters.. read the [iconv](http://php.net/manual/en/function.iconv.php) comments for `//IGNORE` problems which will make your code throw E_NOTICE errors. [mb_convert_encoding](http://php.net/manual/en/function.mb-convert-encoding.php) can only remove invalid characters with `ini_set('mbstring.substitute_character','none');` (see [mbstring.substitute_character](http://php.net/manual/ru/mbstring.configuration.php)) – Sanya_Zol Feb 27 '15 at 22:59
  • 1
    @Sanya_Zol True, but that is because you are using glibc's iconv() implementation. In theory it could work differently on other platforms as [POSIX doesn't define which codesets are available](http://pubs.opengroup.org/onlinepubs/9699919799/functions/iconv_open.html). – pelle Mar 02 '15 at 13:08
  • 1
    `iconv -l` for windows – Pacerier Apr 16 '15 at 05:16
0

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.

webshaker
  • 159
  • 6
  • 4
    link at this bug would be helpful – Tebe Dec 25 '17 at 07:33
  • I don't think it was ever PHP 5.4 bug. We had this when moving app between production servers, there were differences caused by different `iconv` versions on each host machine. – cprn Jun 27 '19 at 10:29