PHP converts the encoding of all files in the folder

Source: Internet
Author: User
The encoding of all files in the PHP conversion folder is suitable for other encoding versions released on the website. for example, if you have a GBK version, you want to have a UTF8 version or you only have the GBK source code. you want to develop it again, but you don't want to change it. you can use this program to convert the encoding method of IDE into UTF8 in batches: the code is as follows:

The encoding of all files in the PHP conversion folder is suitable for other encoding versions released on the website. for example, if you have a GBK version, you want to have a UTF8 version or you only have the GBK source code. you want to develop it again, but you don't want to change it. you can use this program to convert the encoding method of IDE into UTF8 in batches:

The code is as follows:

 

 
 
  1. /**
  2. * You can only transcode all files in a folder once. otherwise, all files are garbled.
  3. * @ Param string $ filename
  4. */
  5. Function iconv_file ($ filename, $ input_encoding = 'gbk', $ output_encoding = 'utf-8 ')
  6. {
  7. If (file_exists ($ filename ))
  8. {
  9. If (is_dir ($ filename ))
  10. {
  11. Foreach (glob ("$ filename/*") as $ key => $ value)
  12. {
  13. Iconv_file ($ value );
  14. }
  15. }
  16. Else
  17. {
  18. $ Contents_before = file_get_contents ($ filename );
  19. /* $ Encoding = mb_detect_encoding ($ contents_before, array ('cp936', 'ascii ', 'gbk', 'gb2312', 'utf-8 '));
  20. Echo $ encoding;
  21. If ($ encoding = 'utf-8') mb_detect_encoding function does not work
  22. {
  23. Return;
  24. }*/
  25. $ Contents_after = iconv ($ input_encoding, $ output_encoding, $ contents_before );
  26. File_put_contents ($ filename, $ contents_after );
  27. }
  28. }
  29. Else
  30. {
  31. Echo 'parameter error ';
  32. Return false;
  33. }
  34. }
  35. Iconv_file ('./test ');
  36. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.