PHP code example for batch deletion and clearing UTF-8 file BOM header_PHP tutorial

WBOY
Release: 2016-07-13 10:32:45
Original
837 people have browsed it

Remember to back up the files before running the code to avoid failure problems.

Code 1:

Copy code The code is as follows:

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) {
                                                                                                                                                                 > rewrite ($filename, $rest);
return ("BOM found, automatically removed.");
} else {
return ("< ;font color=red>BOM found.");
                                                                                                                                            🎜>

Code 2:



Copy code

The code is as follows:


header('content-Type: text/html; charset=utf-8');
if(isset($_GET['dir'])){ / /Set the file directory. If not set, it will automatically be set to the directory where the current file is located
$basedir=$_GET['dir'];
}else{
$basedir='.';
}
$auto=1;/*Set to 1 to detect BOM and remove it, set to 0 to only detect BOM and not remove it*/

echo 'The current directory being searched is: '.$basedir.' The current setting is: ';
echo $auto?' Detect the BOM of the file and remove the BOM of the detected BOM file
': 'Only detect file BOM and do not perform BOM removal
';

checkdir($basedir);
function checkdir($basedir){
if($dh=opendir($basedir)){
while (($file=readdir($dh)) ! == false){
                                                                                                                                                                                                                                        if ($file != '.' && $file != '..') >                        echo 'File: '.$basedir.'/'.$file .checkBOM($basedir.'/'.$file).'
';
                                                                                                                                                                                                    not not less echo 'File:'. basedir.'/'.$file;
                                                                                                                                                                   🎜> }
}
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){
                                                                                                                                                                                                     ;
rewrite($filename,$rest);
return ('BOM found and automatically removed');
}else{
return (' BOM found');
}
}else{
    return ('BOM not found');
}
}
function rewrite($filename,$data){
$filenum=fopen($filename,'w');
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?>




Code three:




Copy code

The code is as follows:


##把该文件放在需求去除BOM头的目录下跑一下却可。
if (isset ( $_GET ['dir'] )) { // config the basedir
    $basedir = $_GET ['dir'];
} else {
    $basedir = '.';
}

$auto = 1;

checkdir ( $basedir );
function checkdir($basedir) {
if ($dh = opendir ( $basedir )) {
while ( ($file = readdir ( $dh )) ! == false ) {
                                                                                                                                                                              ​If it is a file
echo "filename: $basedir/$file " . checkBOM ( "$basedir/$file" ) . "
";
} else {
$dirname = $basedir . "/" . $file; // If it is a directory
                                                                                                                                                      ( $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
                                                                                                                                  // code separation for
                                                                                                                                                                                                                                                                            $rest = substr ($contents, 3);
rewrite ( $filename, $rest );
        return ("BOM found, automatically removed.");
                                                       use           use       use using using using using using using       out out out out out out out out out out ‘’’ out. =red>BOM found.");
}
} else
                                                                                                                                                                                                                                                            ; data) {
$filenum = fopen ($filename, "w");
flock ($filenum, LOCK_EX);
fwrite ($filenum, $data);
fclose ($filenum) ;
}
?>








2. Python

Copy code

The code is as follows:


#!/usr/bin/env python
#-*- coding: utf-8 -*-

import os

def delBOM():
 file_count = 0
 bom_files  = []

 for dirpath, dirnames, filenames in os.walk('.'):
  if(len(filenames)):
   for filename in filenames:
    file_count += 1
    file = open(dirpath + "/" + filename, 'r+')
    file_contents = file.read()

    if(len(file_contents) > 3):
     if(ord(file_contents[0]) == 239 and ord(file_contents[1]) == 187 and ord(file_contents[2]) == 191):
      bom_files.append(dirpath + "/" + filename)
      file.seek(0)
      file.write(file_contents[3:])
      print bom_files[-1], "BOM found. Deleted."
    file.close()

 print file_count, "file(s) found.", len(bom_files), "file(s) have a bom. Deleted."

if __name__ == "__main__":
 delBOM()

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/754341.htmlTechArticle记得运行代码前先把文件备份一下哦,避免出现失败问题。 代码一: 复制代码 代码如下: function checkBOM ($filename) { global $auto; $contents = fil...
Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template