The file encoding method for mb_detect_encoding detection in php [not perfect]

Source: Internet
Author: User
Tags foreach manual md5

For the file encoding detection, Baidu regards it all, but it does not work,
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 php manual explains this as follows:

Mb_detect_encoding-encoding of detected characters, string mb_detect_encoding (string $ str [, mixed $ encoding_list = mb_detect_order () [, bool $ strict = false])

This function has three parameters:

1. str: the string www.111cn.net to be checked.

2. encoding_list: encoding_list is a character encoding list. The encoding sequence can be specified by list strings separated by arrays or commas.

If encoding_list is omitted, detect_order is used.

3. strict: strict specifies whether the encoding is strictly checked. The default value is FALSE.

The following is an example:

The code is as follows: Copy code

$ Encode = mb_detect_encoding ($ keytitle, array ('ascii ', 'gb2312', 'gbk', 'utf-8 '));

The three parameters are: the input variable to be detected. The encoding method check sequence (if the encoding method is true, it is automatically ignored later ).

The strict mode adjusts the encoding detection sequence to minimize the possibility of conversion errors.

In general, the first line of gb2312, when there is GBK and UTF-8, you need to commonly used to sort to the front


Complete instance

The code is as follows: Copy code

<? Php
/**
* 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 "";
}

Finally, we recommend a php detection character encoding mb_detect_encoding () function.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.