PHP conversion folder all file encoding implementation code _ PHP Tutorial

Source: Internet
Author: User
PHP converts the implementation code of all file codes in the folder. 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. if you want to develop a PHP conversion folder again the encoding of all files 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 perform further development, but you do not want to change the IDE encoding method. you can use this program to batch convert it to UTF8:
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 ');
?>


Others are suitable for publishing other website encoding versions. for example, if you have a GBK version, you want to have a UTF8 version, or if you only have the GBK source code, you want to develop it again...

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.