php自动识别文字编码并转换

WBOY
Release: 2016-07-25 08:53:45
Original
689 people have browsed it
  1. /**
  2. * @ string 需要转换的文字
  3. * @ encoding 目标编码
  4. **/
  5. function detect_encoding($string,$encoding = 'gbk'){
  6. $is_utf8 = preg_match('%^(?:[\x09\x0a\x0d\x20-\x7e]| [\xc2-\xdf][\x80-\xbf]| \xe0[\xa0-\xbf][\x80-\xbf] | [\xe1-\xec\xee\xef][\x80-\xbf]{2} | \xed[\x80-\x9f][\x80-\xbf] | \xf0[\x90-\xbf][\x80-\xbf]{2} | [\xf1-\xf3][\x80-\xbf]{3} | \xf4[\x80-\x8f][\x80-\xbf]{2} )*$%xs', $string);
  7. if($is_utf8 && $encoding == 'utf8'){
  8. return $string;
  9. }elseif($is_utf8){
  10. return mb_convert_encoding($string, $encoding, "utf-8");
  11. }else{
  12. return mb_convert_encoding($string, $encoding, 'gbk,gb2312,big5');
  13. }
  14. }
复制代码


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!