PHP Conversion folder for all file coding code _php Tips

Source: Internet
Author: User
The encoding of all files in the PHP transformation folder is suitable for publishing other coded versions of the site like you have a GBK version you want to have a UTF8 version or you only have GBK source code you want to develop two times but you don't want to change the IDE's encoding you can use this program to convert it to UTF8 in bulk:
The code is as follows:
Copy Code code as follows:

<?php
/**
* All the files in a folder can be transferred only once or all of them become 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 ');
?>

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.