We often have to convert UFT-8 characters into gbk or gb2312 encoding, but we often encounter some problems in the conversion process. Let me introduce the use of PHP's own string conversion function to realize characters. Transcoding.
A bug in the iconv function. iconv will make an error when converting the character "-" to gb2312
The solution is very simple, just add “//IGNORE” after the encoding that needs to be converted, which is the second parameter of the iconv function.
As follows:
The following is the quoted content:
The code is as follows
代码如下 |
复制代码 |
iconv(“UTF-8″,”GB2312//IGNORE”,$data)
|
|
Copy code
|
iconv("UTF-8″,"GB2312 //IGNORE”,$data)
代码如下 |
复制代码 |
echo $str= '你好,这里是卖咖啡!';
echo ' ';
echo iconv('GB2312', 'UTF-8', $str); //将字符串的编码从GB2312转到UTF-8
echo ' ';
echo iconv_substr($str, 1, 1, 'UTF-8'); //按字符个数截取而非字节
print_r(iconv_get_encoding()); //得到当前页面编码信息
echo iconv_strlen($str, 'UTF-8'); //得到设定编码的字符串长度
//也有这样用的
$content = iconv("UTF-8","gbk//TRANSLIT",$content);
?>
|
ignore means to ignore errors during conversion. Without the ignore parameter, all strings following this character cannot be saved.
The iconv() function is built-in in php5.
Lie Zi
The code is as follows
|
Copy code
代码如下 |
复制代码 |
function phpcharset($data, $to) {
if(is_array($data)) {
foreach($data as $key => $val) {
$data[$key] = phpcharset($val, $to);
}
} else {
$encode_array = array('ASCII', 'UTF-8', 'GBK', 'GB2312', 'BIG5');
$encoded = mb_detect_encoding($data, $encode_array);
$to = strtoupper($to);
if($encoded != $to) {
$data = mb_convert_encoding($data, $to, $encoded);
}
}
return $data;
}
?>
|
|
|
代码如下 |
复制代码 |
function asciitog($brand)
{
$cha=mb_detect_encoding($brand);
if($cha=='utf-8')
{
$brand2 = iconv($cha,"gb2312",$brand);
}
$cha2=mb_detect_encoding($brand2);
if($cha2!='ascii'){
$brand=$brand2;
}
return $brand;
}
|
echo $str= 'Hello, we sell coffee here!'; |
echo '
';
echo iconv('GB2312', 'UTF-8', $str); //Convert the string encoding from GB2312 to UTF-8
echo '
';
echo iconv_substr($str, 1, 1, 'UTF-8'); //Truncate by the number of characters instead of bytes
print_r(iconv_get_encoding()); //Get the encoding information of the current page
echo iconv_strlen($str, 'UTF-8'); //Get the string length of the set encoding
//This is also used
$content = iconv("UTF-8","gbk//TRANSLIT",$content);
?>
However, when using the iconv function, you may encounter notices such as: iconv() [function.iconv]: detected an illegal character in input string... Error,
The reason is because of the encoding range problem. gb2312 is smaller than gbk and uft8, so everyone should pay attention when converting. However, our php also provides a function mb_detect_encoding which can better solve this problem.
Now write it into a more professional function
The code is as follows
|
Copy code
|
function phpcharset($data, $to) {<🎜>
if(is_array($data)) {<🎜>
foreach($data as $key => $val) {<🎜>
$data[$key] = phpcharset($val, $to);<🎜>
}<🎜>
} else {<🎜>
$encode_array = array('ASCII', 'UTF-8', 'GBK', 'GB2312', 'BIG5');<🎜>
$encoded = mb_detect_encoding($data, $encode_array);<🎜>
$to = strtoupper($to);<🎜>
if($encoded != $to) {<🎜>
$data = mb_convert_encoding($data, $to, $encoded);<🎜>
}<🎜>
}<🎜>
return $data;<🎜>
}<🎜>
?>
Sometimes we don’t know the encoding of a character, and we need to detect the encoding first and then convert it.
The code is as follows
|
Copy code
|
function asciitog($brand)
{
$cha=mb_detect_encoding($brand);
if($cha=='utf-8')
{
$brand2 = iconv($cha,"gb2312",$brand);
}
$cha2=mb_detect_encoding($brand2);
if($cha2!='ascii'){
$brand=$brand2;
}
return $brand;
}
http://www.bkjia.com/PHPjc/632143.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632143.htmlTechArticleWe often encounter the problem of converting UFT-8 characters into gbk or gb2312 encoding, but during use We often encounter some problems. Let me introduce how to use the string conversion function that comes with PHP...
|
|