Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use mb_* or iconv_* functions for multibyte strings?

As we all now, handling multibyte strings is not that easy in PHP. For example I want to get the length of the following string: ä

strlen('ä'); // 2, because ä equals 2 bytes
mb_strlen('ä', 'UTF-8'); // 1
iconv_strlen('ä', 'UTF-8'); // 1

Which functions should I use? The mb_* or iconv_*? Why? Considering that the encoding may not be limited to UTF-8.

Thx in advance!

like image 874
Philippe Gerber Avatar asked Jan 22 '26 21:01

Philippe Gerber


1 Answers

Have a look at this Powerpoint presentation:

http://www.nyphp.org/content/presentations/smallworld/April2006-nyphp-Presentation.ppt

In a nutshell: Iconv supports more encodings, but is less portable.

From the presentation:

PHP supports multi byte in two extensions: iconv and mbstring

  • iconv uses an external library (supports more encodings but less portable)
  • mbstring has the library bundled with PHP (less encodings but more portable)
like image 127
karim79 Avatar answered Jan 25 '26 13:01

karim79