Home > Backend Development > PHP Tutorial > Example of how to convert the encoding of all files in a folder with PHP

Example of how to convert the encoding of all files in a folder with PHP

WBOY
Release: 2016-07-25 08:59:13
Original
796 people have browsed it
  1. /**
  2. * Convert the encoding of all files in the folder
  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. ?>
复制代码


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