问题
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