search
Homephp教程php手册php实现utf-8和GB2312编码相互转换函数代码

php实现utf-8和GB2312编码相互转换函数代码

Jun 13, 2016 am 11:54 AM
getphpuutf-8codefunctionandcopyaccomplishcodingConvert

复制代码 代码如下:


 /********************************************
  *
  * 函数名:get_utf8_to_gb($value)
  * 作  用:utf8编码字符串转换成gb2312编码
  * 作  者:刘先忠
  * 日  期:2011-11-09
  *
  ********************************************/
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;
  }
 }
 /********************************************
  *
  * 函数名:get_gb_to_utf8($value)
  * 作  用:gb2312编码字符串转换成utf8编码
  * 作  者:刘先忠
  * 日  期:2011-11-09
  *
  ********************************************/
 function   get_gb_to_utf8($value){
  $value_1= $value;
  $value_2   =   @iconv( "gb2312", "utf-8//IGNORE",$value_1);
  $value_3   =   @iconv( "utf-8", "gb2312//IGNORE",$value_2);
  if   (strlen($value_1)   ==   strlen($value_3))
  {
   return   $value_2;
  }else
  {
   return   $value_1;
  }
 }
 ?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.