问题描述
当您拥有不同于UTF-8的字符集并且需要将其放入JSON格式以将其迁移到数据库时,PHP中可以使用两种方法调用utf8_encode()和iconv().我想知道哪个具有更好的性能,什么时候可以方便地使用一个或另一个.
when you have a charset different of UTF-8 and you need to put it on JSON format to migrate it to a DB, there are two methods that can be used in PHP, calling utf8_encode() and iconv(). I would like to know which one have better performance, and when is convenient to use one or another.
推荐答案
否-utf8_encode()
仅仅适用于将ISO-8859-1字符串转换为UTF-8的情况. Iconv提供了大量的源和目标编码.
Nope - utf8_encode()
is suitable only for converting a ISO-8859-1 string to UTF-8. Iconv provides a vast number of source and target encodings.
关于性能,我不知道utf8_encode()
在内部如何工作以及它使用的是什么库,但是我的预测不会有太大的区别-至少对于字节或字节的正常"数据量没有影响千字节.如有疑问,请进行基准测试.
Re performance, I have no idea how utf8_encode()
works internally and what libraries it uses, but my prediction is there won't be much of a difference - at least not on "normal" amounts of data in the bytes or kilobytes. If in doubt, do a benchmark.
我倾向于使用iconv()
,因为很明显,从字符集A到字符集B都有转换.
I tend to use iconv()
because it's clearer that there is a conversion from character set A to character set B.
此外,iconv()
在遇到无效数据时提供了更详细的控制.将//IGNORE
添加到目标字符集将导致它静默删除无效字符.在某些情况下这可能会有所帮助.
Also, iconv()
provides more detailed control on what to do when it encounters invalid data. Adding //IGNORE
to the target character set will cause it to silently drop invalid characters. This may be helpful in certain situations.
这篇关于iconv()与utf8_encode()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!