Copy code The code is as follows:
iconv('GBK', 'UTF-8//IGNORE', 'Script home'); // Convert the string from GBK encoding to UTF-8 encoding
But iconv can only solve the situation where the encoding is known in advance. If the string encoding is unknown, you need to detect its encoding first. In this case, the mb_string extension library may be used:
Copy code The code is as follows:
mb_detect_encoding('Script Home');
However, mb_detect_encoding has a flaw, and inaccurate judgment often occurs. Maybe this can solve it:
Copy code The code is as follows:
// Use iconv to convert and determine whether it is equivalent, which is not efficient
function is_utf8 ($str) {
if ($str === iconv('UTF-8', 'UTF-8//IGNORE', $str)) {
return 'UTF-8';
}
}
// Multiple encoding situations
function detect_encoding ($str) {
foreach (array('GBK', 'UTF-8') as $v) {
If ($str === iconv($v, $v . '//IGNORE', $str)) {
🎜>
After obtaining the string encoding information through the above method, you can use iconv or mb_convert_encoding to convert the encoding.
http://www.bkjia.com/PHPjc/774997.html
www.bkjia.com
truehttp: //www.bkjia.com/PHPjc/774997.htmlTechArticleCopy the code as follows: iconv('GBK', 'UTF-8//IGNORE', 'Script Home '); //Convert the string from GBK encoding to UTF-8 encoding but iconv can only solve the situation where the encoding is known in advance, if...