Home  >  Article  >  Backend Development  >  How to convert utf8 to gb2312 in php

How to convert utf8 to gb2312 in php

coldplay.xixi
coldplay.xixiOriginal
2020-09-04 11:42:394271browse

php method to convert utf8 to gb2312: use the ICONV function to avoid that a certain character does not have a corresponding character in the target character set. The part after this character will be ignored. The code is [iconv( "UTF- 8", "gb2312", "abc sunshine 123");].

How to convert utf8 to gb2312 in php

[Related learning recommendations: php programming (video)]

How to convert utf8 to gb2312 in php:

Use ICONV function: iconv( "UTF-8", "gb2312", "abc Sunshine 123");

<?php
 * 函数名:get_utf8_to_gb($value)
 * 作  用:utf8编码字符串转换成gb2312编码
function   get_utf8_to_gb($value)
{  $value_1= $value;  
 $value_2=@iconv( "utf-8", "gb2312//IGNORE",$value_1);
 //使用@抵制错误,如果转换字符串中,某一个字符在目标字符集里没有对应字符,那么,这个字符之后的部分就被忽略掉了;
 即结果字符串内容不完整,此时要使用//IGNORE 
 $value_3=@iconv( "gb2312", "utf-8//IGNORE",$value_2); 
 if(strlen($value_1)==strlen($value_3))
 {   return   $value_2;
 }else
 {   return   $value_1;
 }
}
?>

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of How to convert utf8 to gb2312 in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn