Search code examples
phputf-8utf-16iconvmbstring

UTF-8 to UTF-16, different results using iconv vs mbstring


I'm getting different results in php when using iconv vs mb_convert_encoding when trying to convert UTF-8 to UTF-16.

echo iconv('UTF-8', 'UTF-16', 'test'); // ��test

echo mb_convert_encoding('test', 'UTF-16', 'UTF-8'); // test

Notice the two � symbols at the beginning of the beginning of the iconv() output.

Any thoughts on why mb_convert_encoding isn't doing the same?

Thanks.


Solution

  • iconv adds a BOM at the begging of the output string. So for converting string, you probably want to use mb_convert_encoding. iconv can be more useful for files.