I wrote a php function to detect file encoding.

Source: Internet
Author: User
For file encoding detection, Baidu has written a detection function, but it does not work. The following describes how to check file encoding, baidu is a hacker, but it is useless,
Many people suggest mb_detect_encoding detection, but I don't know why I didn't succeed, and I didn't output anything,
I saw someone wrote an enhanced version, and I decided to ignore it with BOM. this is totally unreliable,
Finally, according to the example below the mb_detect_encoding function in the PHP manual, I wrote a detection function,
It also includes functions that automatically detect the encoding and read the file according to the instruction code,
The source code is presented.
I tried to write the online method without using it. maybe the environment is different.
So if it's useless, don't spray me, I just want to share ideas ,,
The code is as follows:
/**
* Check the file encoding.
* @ Param string $ file path
* @ Return string | null returns the encoding name or null.
*/
Function detect_encoding ($ file ){
$ List = array ('gbk', 'utf-8', 'utf-16le', 'utf-16BE ', 'ISO-8859-1 ');
$ Str = file_get_contents ($ file );
Foreach ($ list as $ item ){
$ Tmp = mb_convert_encoding ($ str, $ item, $ item );
If (md5 ($ tmp) = md5 ($ str )){
Return $ item;
}
}
Return null;
}

/**
* Automatically parse the encoding to read the file
* @ Param string $ file path
* @ Param string $ charset read encoding
* @ Return string returns the read content
*/
Function auto_read ($ file, $ charset = 'utf-8 '){
$ List = array ('gbk', 'utf-8', 'utf-16le', 'utf-16BE ', 'ISO-8859-1 ');
$ Str = file_get_contents ($ file );
Foreach ($ list as $ item ){
$ Tmp = mb_convert_encoding ($ str, $ item, $ item );
If (md5 ($ tmp) = md5 ($ str )){
Return mb_convert_encoding ($ str, $ charset, $ item );
}
}
Return "";
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.