Heim  >  Artikel  >  php教程  >  php自动识别编码转换为UTF-8

php自动识别编码转换为UTF-8

WBOY
WBOYOriginal
2016-06-06 20:08:381013Durchsuche

今天发现一段很好的代码: function characet($data) if( !empty($data) ) $fileType = mb_detect_encoding($data , array(UTF-8,GBK,LATIN1,BIG5)) ; if( $fileType != UTF-8) $data = mb_convert_encoding($data ,utf-8 , $fileType); return $data; 这个

今天发现一段很好的代码:

function characet($data){
  if( !empty($data) ){    
    $fileType = mb_detect_encoding($data , array('UTF-8','GBK','LATIN1','BIG5')) ;  
    if( $fileType != 'UTF-8'){  
      $data = mb_convert_encoding($data ,'utf-8' , $fileType);  
    }  
  }  
  return $data;    
}



这个在于传中文参数的时候,由于编码不同,会发生乱码现象,当参数编码不一定是GBK的时候,那就不能都转换编码了
上面的好处是,判断编码,然后转换,和谐了
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn