Home > Article > Backend Development > PHP method to determine whether a string encoding is utf8 and convert it_PHP tutorial
Regarding the problem of garbled strings under PHP, when there are two encodings on a page, garbled characters will appear no matter which encoding you choose. So how can we solve this problem? The method provided by the editor of Liehuo:
1. First, according to international practice, check whether your file encoding is consistent with the database and page encoding.
Two. If there is no problem with "one", then you need to determine the encoding of the string first. We only need to determine one type. Regarding the issue of detecting whether it is utf8 encoding, Bangkejia has published related articles before, but it is not very good. Use it. I found a relatively perfect function on the Internet today. The code is as follows:
Copy to ClipboardQuoted content: [www.bkjia.com] function is_utf8($liehuo_net)Now let’s talk about how to use it. Experts don’t need to read it, because some beginners don’t know how to introduce and use this function. The following is the method.
1. Save the above code as a separate file, for example: liehuo_func_validt.php, save it to the root directory of the website, then import it into the file and add the following code:
Copy to ClipboardQuoted content: [www.bkjia.com] require( "liehuo_func_validt.php" );Next, judge the code and write it below the above code:
Copy to ClipboardQuoted content: [www.bkjia.com] if(is_utf8($liehuo)==1)That’s it.
Two. Put the judgment function directly at the top of the file, and then add the judgment code, which is more verbose, haha. Pay more attention to Bangke Home and support Liehuo.Net.