Conversion and Determination of string encoding in PHP learning notes

Source: Internet
Author: User

Copy codeThe Code is as follows:
Iconv ('gbk', 'utf-8 // IGNORE ', 'feet home'); // converts a string from GBK encoding to UTF-8 Encoding

However, iconv can only solve the problem of pre-known encoding. If the string encoding is unknown, you need to first detect its encoding. In this case, the mb_string extension library may be used:

Copy codeThe Code is as follows:
Mb_detect_encoding ('foot home ');

However, mb_detect_encoding has a hard injury, which often results in inaccurate judgment. This may solve the problem:

Copy codeThe Code is as follows:
// Use iconv to convert and determine whether the value is equal. The efficiency is not high.
Function is_utf8 ($ str ){
If ($ str = iconv ('utf-8', 'utf-8 // IGNORE ', $ str )){
Return 'utf-8 ';
}
}
// Multiple encoding conditions
Function detect_encoding ($ str ){
Foreach (array ('gbk', 'utf-8') as $ v ){
If ($ str = iconv ($ v, $ v. '// IGNORE', $ str )){
Return $ v;
}
}
}

After obtaining the string encoding information, you can use iconv or mb_convert_encoding to convert the encoding information.

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.