PHP Batch Conversion folder under All file encoding

Source: Internet
Author: User
All file encodings under PHP conversion folder

The encoding of all files under the PHP conversion folder is suitable for other encoded versions of the publishing site for example, 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 way the IDE is encoded you can use this program to convert it to UTF8 in batches:

The code is as follows:

/** * All the files in a folder can be transcoded only once otherwise all 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.