PHP converts the encoding of all files in the folder Suitable for publishing other encoding versions of the website

WBOY
Release: 2016-07-25 09:05:56
Original
920 people have browsed it
The encoding of all files in the PHP conversion folder is suitable for publishing other encoding versions of the website. For example, if you have a GBK version and you want to have a UTF8 version, or you only have the source code of GBK and you want secondary development but you don’t want to change the encoding method of the IDE, you can use This program will batch convert it to UTF8
  1. /**
  2. * Convert all the files in a folder. You can only convert them once, otherwise they will all become garbled. * @param string $filename
  3. */
  4. function iconv_file($filename,$input_encoding='gbk',$output_encoding='utf-8')
  5. {
  6. if(file_exists($filename) )
  7. {
  8. if(is_dir($filename))
  9. {
  10. foreach (glob("$filename/*") as $key=>$value)
  11. {
  12. iconv_file($value);
  13. }
  14. }
  15. else
  16. {
  17. $contents_before = file_get_contents($filename);
  18. /*$encoding = mb_detect_encoding($contents_before,array('CP936','ASCII','GBK','GB2312','UTF-8'));
  19. echo $encoding;
  20. if($encoding=='UTF-8') mb_detect_encoding function not working
  21. {
  22. return;
  23. }*/
  24. $contents_after = iconv($input_encoding,$output_encoding,$contents_before);
  25. file_put_contents($ filename, $contents_after);
  26. }
  27. }
  28. else
  29. {
  30. echo 'Parameter error';
  31. return false;
  32. }
  33. }
  34. iconv_file('./test');
  35. ?>
Copy code

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!