


PHP code example for batch deletion and clearing UTF-8 file BOM header_PHP tutorial
Jul 13, 2016 am 10:32 AM
Remember to back up the files before running the code to avoid failure problems.
Code 1:
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 ("<font color=red>BOM found, automatically removed.</font>");
} else {
return ("< ;font color=red>BOM found.</font>");
🎜>
Code 2:
The code is as follows: echo 'The current directory being searched is: '.$basedir.' The current setting is: '; checkdir($basedir);
<?php
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 $auto?' Detect the BOM of the file and remove the BOM of the detected BOM file<br />': 'Only detect file BOM and do not perform BOM removal<br />';
function checkdir($basedir){
if($dh=opendir($basedir)){
while (($file=readdir($dh)) ! == false){
if ($file != '.' && $file != '..') > echo 'File: '.$basedir.'/'.$file .checkBOM($basedir.'/'.$file).' <br>';
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 ('<font color=red>BOM found and automatically removed</font>');
}else{
return (' <font color=red>BOM found</font>');
}
}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
##把该文件放在需求去除BOM头的目录下跑一下却可。
<?php
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" ) . " <br>";
} 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 ("<font color=red>BOM found, automatically removed.</font>");
use use use using using using using using using out out out out out out out out out out ‘’’ out. =red>BOM found.</font>");
}
} 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()

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
