PHP method for determining whether string encoding is utf8 and conversion _ PHP Tutorial

Source: Internet
Author: User
PHP determines whether the string encoding is utf8 and the conversion method. About the character string garbled in PHP, when a page contains two types of encoding, no matter what you choose, there will be garbled characters. how can this problem be solved? The fire editor gave a question about character string garbled characters in PHP. when a page contains two types of codes, no matter what you choose, there will be garbled characters. how can this problem be solved? The solution provided by the fire editor is as follows:

I. First, check whether your file encoding is consistent with the database and page encoding according to international practices.

II. If there is no problem with "1", we need to determine the encoding of the string first. we only need to determine whether the encoding is utf8, the helper House has published related articles before, but it is not very useful. today, I found a perfect function on the internet. the code is as follows:

Copy to ClipboardReference: [www.bkjia.com] function is_utf8 ($ liehuo_net)
{
If (preg_match ("/^ ([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1}/", $ liehuo_net) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {1} $/", $ liehuo_net) = true | preg_match ("/([". chr (1, 228 ). "-". chr (1, 233 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1 }[". chr (1, 128 ). "-". chr (1, 191 ). "] {1}) {2,}/", $ liehuo_net) = true)
{
Return true;
}
Else
{
Return false;
}

}

Now let's talk about how to use this function. you don't need to read it, because some beginners don't know how to introduce and use this function. below is the method.

I. Save the preceding code as an independent file, for example, liehuo_func_validt.php, save it to the root directory of the website, then introduce it to the file, and add the following code:

Copy to ClipboardReference: [www.bkjia.com] require ("liehuo_func_validt.php ");

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

Copy to ClipboardReference: [www.bkjia.com] if (is_utf8 ($ liehuo) = 1)
{
$ Liehuo = iconv ("UTF-8", "gbk", $ liehuo );
}

In this way, you can.

II. Place the judgment function directly on the top of the file and add the judgment code. it's cool, haha. Focus more on helping customers and support Liehuo. Net.

Why? Fire Xiaobian...

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.