php implements utf 8 and GB2312 encodings to convert function codes to each other

  • 2020-05-30 19:41:29
  • OfStack


<?php
 /********************************************
  *
  *  The function name: get_utf8_to_gb($value)
  *  As a    To use: utf8 The encoding string is converted to gb2312 coding 
  *  As a    Reporter: liu xianzhong 
  *  day    Time: 2011-11-09 
  *
  ********************************************/
function   get_utf8_to_gb($value){
  $value_1= $value;
  $value_2   =   @iconv( "utf-8", "gb2312//IGNORE",$value_1);// use @ Resist errors if you convert something in a string 1 If there is no corresponding character in the target character set, the part after this character is ignored. That is, the result string content is incomplete, at this point to use //IGNORE 
  $value_3   =   @iconv( "gb2312", "utf-8//IGNORE",$value_2);

 if   (strlen($value_1)   ==   strlen($value_3))
  {
   return   $value_2;
  }else
  {
   return   $value_1;
  }
 }
 /********************************************
  *
  *  The function name: get_gb_to_utf8($value)
  *  As a    To use: gb2312 The encoding string is converted to utf8 coding 
  *  As a    Reporter: liu xianzhong 
  *  day    Time: 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;
  }
 }
 ?>

Related articles: