search
Homephp教程PHP源码批量清除BOM头
批量清除BOM头May 23, 2016 pm 05:09 PM

跳至

<?php  
//有些php文件由于不小心保存成了含bom头的格式而导致出现一系列的问题。以下是批量清除bom头的代码

if (isset ( $_GET [&#39;dir&#39;] )) { //config the basedir    
    $basedir = $_GET [&#39;dir&#39;];    
} else {    
    $basedir = &#39;.&#39;;    
}    
$basedir="./weibo/";
$auto = 1;    

checkdir ( $basedir );    

function checkdir($basedir) {    
    if ($dh = opendir ( $basedir )) {    
        while ( ($file = readdir ( $dh )) !== false ) {    
            if ($file != &#39;.&#39; && $file != &#39;..&#39;) {    
                if (! is_dir ( $basedir . "/" . $file )) { // 如果是文件    
                    echo "filename: $basedir/$file " . checkBOM ( "$basedir/$file" ) . " 
";    
                } else {    
                    $dirname = $basedir . "/" .$file; // 如果是目录    
                    checkdir ( $dirname ); // 递归    
                }    
            }    
        }    
        closedir ( $dh );    
    }    
}    

function checkBOM($filename) {    
    global $auto;    
    $contents = file_get_contents ( $filename );    
    $charset [1] = substr ( $contents, 0, 1 );    
    $charset [2] = substr ( $contents, 1, 1 );    
    $charset [3] = substr ( $contents, 2, 1 );    
    if (ord ( $charset [1] ) == 239 && ord ( $charset [2] ) == 187 && ord ( $charset [3] ) == 191) { // BOM 的前三个字符的ASCII 码分别为 239 187 191    
        if ($auto == 1) {    
            $rest = substr ( $contents, 3 );    
            rewrite ( $filename, $rest );    
            return ("");    
        } else {    
            return ("");    
        }    
    } else    
        return ("BOM Not Found.");    
}    

function rewrite($filename, $data) {    
    $filenum = fopen ( $filename, "w" );    
    flock ( $filenum, LOCK_EX );    
    fwrite ( $filenum, $data );    
    fclose ( $filenum );    
}

                   

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尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!