9

iconv() and mb_convert_encoding() in PHP? - Stack Overflow

 7 years ago
source link: http://stackoverflow.com/questions/8233517/what-is-the-difference-between-iconv-and-mb-convert-encoding-in-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.

Asked 8 years, 7 months ago
Viewed 20k times

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?

asked Nov 22 '11 at 20:39

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.

answered Aug 26 '14 at 15:53

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.

answered Mar 12 '16 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged php character-encoding special-characters or ask your own question.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK