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