PHP code for converting all file codes in a folder

Source: Internet
Author: User
This article provides a detailed analysis of the implementation code of all file encoding in the PHP conversion folder. For more information, see

This article provides a detailed analysis of the implementation code of all file encoding in the PHP conversion folder. For more information, see

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 Code is as follows:


/**
* You can only transcode all files in a folder once. Otherwise, all files are garbled.
* @ Param string $ filename
*/
Function iconv_file ($ filename, $ input_encoding = 'gbk', $ output_encoding = 'utf-8 ')
{
If (file_exists ($ filename ))
{
If (is_dir ($ filename ))
{
Foreach (glob ("$ filename/*") as $ key => $ value)
{
Iconv_file ($ value );
}
}
Else
{
$ Contents_before = file_get_contents ($ filename );
/* $ Encoding = mb_detect_encoding ($ contents_before, array ('cp936', 'ascii ', 'gbk', 'gb2312', 'utf-8 '));
Echo $ encoding;
If ($ encoding = 'utf-8') mb_detect_encoding function does not work
{
Return;
}*/
$ Contents_after = iconv ($ input_encoding, $ output_encoding, $ contents_before );
File_put_contents ($ filename, $ contents_after );
}
}
Else
{
Echo 'parameter error ';
Return false;
}
}
Iconv_file ('./test ');
?>


, Hong Kong Space, U.S. servers, and Hong Kong Virtual Hosts

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.