How do I convert files between encodings where only some of them are wrong?

梦想的初衷 提交于 2019-12-02 08:40:09

问题


I have a large set of nested directories containing PHP, HTML, and Javascript files that should all be encoded as UTF-8. However, someone edited several of the files and saved them with ISO-8859-1 encoding. Unfortunately, they're all mixed in with the UTF-8 files.

I'd like to use the iconv tool to convert the incorrectly-encoded files to UTF-8 (as described here). Primarily, the problems occur with characters that are valid ISO-8859-1 but invalid UTF-8.

I think an appropriate starting point would be to find all files that contain invalid UTF-8. What's a good way to do this?

I realise this won't catch all of the cases where the wrong character might be displayed. Any further tips on how I might fix this mess?


回答1:


This would be a bit of a hack, but since it's a one-off occurrence then it might be worth it. iconv will complain about invalid encoding if it can't read the file using the encoding you give it. Therefore, you could write a wrapper script to iterate over all the files, attempting to convert them from UTF-8 to something else, and those that can't be converted have invalid UTF-8.



来源:https://stackoverflow.com/questions/12726517/how-do-i-convert-files-between-encodings-where-only-some-of-them-are-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!