PHP how to detect the file encoding, the user uploads a file, the page shows the content, the entire website is UTF-8, if the user uploads a GB file, it shows garbled. You can use mb_convert_encoding ($ text, & nbsp; & quot; UTF-8 & quot;, & nbsp; & quot; GBK & quot;); to convert, but how can we determine whether the user uploads a GB-encoded file? --- How does PHP detect file encoding?
The user uploads a file, the page shows the content, the whole website is UTF-8, if the user uploads a GB file, it shows garbled.
You can use mb_convert_encoding ($ text, "UTF-8", "GBK"); to convert, but how do you determine that the user uploads a GB-encoded file?
------ Solution --------------------
Mb_check_encoding -- Check if the string is valid for the specified encoding
------ Solution --------------------
It seems that an error has been written upstairs.
Mb_detect_encoding ($ string)
------ Solution --------------------
Iconv can also be ....
------ Solution --------------------
$ Temstr = file_get_contents ($ path );
$ Encode = mb_detect_encoding ($ temstr, "ASCII, UTF-8, CP936, EUC-CN, BIG-5, EUC-TW ");
$ Temstr = mb_convert_encoding ($ temstr, "CP936", $ encode );