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:
- /**
- * 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 ');
- ?>