PHP Learning Note string encoding conversion and judgment _php Tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
Iconv (' GBK ', ' utf-8//ignore ', ' home of the script '); Converts a string from GBK encoding to UTF-8 encoding

However, Iconv can only solve the pre-known encoding situation, if the string encoding is unknown, you need to first probe its encoding, you may use the Mb_string extension library:

Copy the Code code as follows:
Mb_detect_encoding (' Home of the script ');

But mb_detect_encoding there is a mishap, often appear to judge inaccurate situation. Perhaps this will solve:

Copy the Code code as follows:
Use Iconv to convert and determine whether the equivalence is not high efficiency
function Is_utf8 ($STR) {
if ($str = = = Iconv (' UTF-8 ', ' Utf-8//ignore ', $str)) {
Return ' UTF-8 ';
}
}
Multiple coding scenarios
function detect_encoding ($STR) {
foreach (Array (' GBK ', ' UTF-8 ') as $v) {
if ($str = = = Iconv ($v, $v. '//ignore ', $str)) {
return $v;
}
}
}

After getting the string encoding information in the above way, you can use Iconv or mb_convert_encoding to convert the encoding.

http://www.bkjia.com/PHPjc/774997.html www.bkjia.com true http://www.bkjia.com/PHPjc/774997.html techarticle Copy the code as follows: Iconv (' GBK ', ' utf-8//ignore ', ' home of the script ');//Convert the string from GBK encoding to UTF-8 encoding, but Iconv can only resolve the case where the encoding is known beforehand, if ...

  • 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.