Home >Backend Development >PHP Tutorial >Function to automatically detect encodings in content and convert them

Function to automatically detect encodings in content and convert them

WBOY
WBOYOriginal
2016-07-25 09:07:08840browse
  1. //---示例

  2. $url = “http://www.163.com”;
  3. $data = file_get_contents($url);
  4. $data = my_encoding($data,”utf-8″);
  5. echo “$data”;

  6. //--函数

  7. function my_encoding($data,$to)
  8. {
  9. $encode_arr = array(‘UTF-8′,’ASCII’,'GBK’,'GB2312′,’BIG5′,’JIS’,'eucjp-win’,’sjis-win’,'EUC-JP’);
  10. $encoded = mb_detect_encoding($data, $encode_arr);
  11. $data = mb_convert_encoding($data,$to,$encoded);
  12. return $data;
  13. }

  14. ?>

复制代码


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