开发者

How can I auto-detect ISO 8859-1 versus UTF-8 encoding in PHP? [duplicate]

This question already has answers here: Detect encoding and make everything UTF-8 (26 answers) Closed 11 months ago.

I have a legacy database table with a mixed encoding. Some lines are UTF-8 and some lines are ISO 8开发者_运维问答859-1.

Are there some heuristics I can apply on the content of a line to guess which encoding best represents the content?


Convert from UTF-8. If that fails then it's not UTF-8, so you should probably convert from Latin-1 instead.


Compare

iconv("UTF-8", "ISO-8859-1//IGNORE", $text)

and

iconv("UTF-8", "ISO-8859-1", $text)

If they are not equal - consider it UTF-8.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜