php轉換資料夾下所有檔案編碼的方法舉例

WBOY
發布: 2016-07-25 08:59:13
原創
731 人瀏覽過
  1. /**
  2. * 转换文件夹所有文件的编码
  3. * @param string $filename
  4. * @edit bbs.it-home.org
  5. */
  6. function iconv_file($filename,$input_encoding='gbk',$output_encoding='utf-8')
  7. {
  8. if(file_exists($filename))
  9. {
  10. if(is_dir($filename))
  11. {
  12. foreach (glob("$filename/*") as $key=>$value)
  13. {
  14. iconv_file($value);
  15. }
  16. }
  17. else
  18. {
  19. $contents_before = file_get_contents($filename);
  20. /*$encoding = mb_detect_encoding($contents_before,array('CP936','ASCII','GBK','GB2312','UTF-8'));
  21. echo $encoding;
  22. if($encoding=='UTF-8') mb_detect_encoding函数不工作
  23. {
  24. return;
  25. }*/
  26. $contents_after = iconv($input_encoding,$output_encoding,$contents_before);
  27. file_put_contents($filename, $contents_after);
  28. }
  29. }
  30. else
  31. {
  32. echo '参数错误';
  33. return false;
  34. }
  35. }
  36. iconv_file('./test');
  37. ?>
复制代码


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!