PHP How to determine if string encoding is UTF8 and converted _php tutorial

Source: Internet
Author: User
About PHP string garbled problem, when a page exists in two kinds of code, no matter how you choose the first choice will appear garbled, then how to solve this problem? Fire small to provide you with the method:

One. First of all, according to international practice, check your file encoding and database, page encoding is consistent.

Two. If the "one" is not a problem, then you need to judge the encoding of the string, we just have to judge a kind of can, about the detection is UTF8 coding problem, help the home has published related articles, but not very good, today found on the Internet a more perfect function, the code is as follows:

Copy to ClipboardWhat to refer to: [www.bkjia.com]function Is_utf8 ($liehuo _net)
{
if (Preg_match ("/^ ([". chr (228). " -". Chr (233)." {1} [". chr (128)." -". Chr (191)." {1} [". chr (128)." -". Chr (191)." {1}) {1}/", $liehuo _net) = = True | | Preg_match ("/[". chr (228). " -". Chr (233)." {1} [". chr (128)." -". Chr (191)." {1} [". chr (128)." -". Chr (191)." {1}) {1}$/", $liehuo _net) = = True | | Preg_match ("/[". chr (228). " -". Chr (233)." {1} [". chr (128)." -". Chr (191)." {1} [". chr (128)." -". Chr (191)." {1}) {2,}/", $liehuo _net) = = True)
{
return true;
}
Else
{
return false;
}

}

Now for the use of the method, the master will not have to look, because some beginners do not know how to introduce and use this function, the bottom is the method.

One. Save the above code as a standalone file, for example: liehuo_func_validt.php, save to the Web site root directory, and then introduce to the file, add the following code:

Copy to ClipboardWhat to refer to: [www.bkjia.com]Require ("liehuo_func_validt.php");

Next, judge the code, and write it down below the code:

Copy to ClipboardWhat to refer to: [www.bkjia.com]if (Is_utf8 ($liehuo) ==1)
{
$liehuo = Iconv ("Utf-8", "GBK", $liehuo);
}

That's all you can do.

Two. Put the judgment function directly at the top of the file to use, and then add the judgment code, more verbose, haha. More attention to help the home, support liehuo.net Oh.

http://www.bkjia.com/PHPjc/364702.html www.bkjia.com true http://www.bkjia.com/PHPjc/364702.html techarticle about PHP string garbled problem, when a page exists in two kinds of code, no matter how you choose the first choice will appear garbled, then how to solve this problem? A small part of the fire.

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