search
Homephp教程PHP源码PHP converts the encoding of all files in the folder

The encoding of all files in the PHP conversion folder is suitable for publishing other encoding versions of the website. For example, if you have a GBK version and you want to have a UTF8 version, or you only have the source code of GBK and you want secondary development but you don’t want to change the encoding method of the IDE, you can Use this program to batch convert it to UTF8:

The code is as follows:

<?php
/**
 * 把一个文件夹里的文件全部转码 只能转一次 否则全部变乱码
 * @param string $filename
 */
function iconv_file($filename,$input_encoding=&#39;gbk&#39;,$output_encoding=&#39;utf-8&#39;)
{
    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(&#39;CP936&#39;,&#39;ASCII&#39;,&#39;GBK&#39;,&#39;GB2312&#39;,&#39;UTF-8&#39;));
            echo $encoding;
            if($encoding==&#39;UTF-8&#39;)  mb_detect_encoding函数不工作
            {
                return;
            }*/
            $contents_after = iconv($input_encoding,$output_encoding,$contents_before);
            file_put_contents($filename, $contents_after);
        }
    }
    else
    {
        echo &#39;参数错误&#39;;
        return false;
    }
}
iconv_file(&#39;./test&#39;);
?>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),