Iconv () Vs. utf8_encode ()

when you have an encoding other than UTF-8, and you need to put it in JSON format to transfer it to the database, there are two methods that you can use in PHP by calling utf8_encode () and iconv (). I would like to know which one has the best performance, and when it is convenient to use one or another.

+8
json php character-encoding
source share
2 answers

when you have encoding other than UTF-8

Nope - utf8_encode() is only suitable for converting an ISO-8859-1 string to UTF-8. Iconv provides a huge number of source and target encodings.

Re, I have no idea how utf8_encode() works internally and in which libraries it uses, but my prediction will not make much difference - at least not for "normal" amounts of data in bytes or kilobytes. If in doubt, do the test.

I prefer to use iconv() because it is clear that there is a conversion from character set A to character set B.

In addition, iconv() provides more granular control over what to do when it encounters invalid data. Adding //IGNORE to the target character set will cause it to silently discard invalid characters. This may be useful in certain situations.

+14
source share

I recommend you write your own function. It will be 2-3 lines in length, and it will be better than fighting languages, iconv problems, etc.

For example: Fix Turkish Charset HTML / PHP error (iconv?)

0
source share

All Articles