PHP batch delete, clear UTF-8 file BOM header code example _ PHP Tutorial

Source: Internet
Author: User
Tags flock
PHP batch delete, clear the code instance of the UTF-8 file BOM header. Remember to back up the file before running the code to avoid failure. Code 1: Copy the code as follows: functioncheckBOM ($ filename) {global $ auto; $ contentsfil remember to back up the file before running the code to avoid failure.

Code 1:

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) {
If ($ auto = 1 ){
$ Rest = substr ($ contents, 3 );
Rewrite ($ filename, $ rest );
Return ("BOM found, automatically removed .");
} Else {
Return ("BOM found .");
}
}
Else return ("BOM Not Found .");
}

Code 2:

The code is as follows:


Header ('content-Type: text/html; charset = utf-8 ');
If (isset ($ _ GET ['dir']) {// sets the file directory. if it is not set, it is automatically set to the directory where the current file is located.
$ Basedir = $ _ GET ['dir'];
} Else {
$ Basedir = '.';
}
$ Auto = 1;/* set to 1: Check BOM and remove it. set to 0: check BOM only */

Echo 'The directory Currently searched is: '. $ basedir.' the current setting is :';
Echo $ auto? 'Check the file BOM while removing the BOM of the detected BOM file
':' Only checks the file BOM and does not delete the BOM.
';

Checkdir ($ basedir );
Function checkdir ($ basedir ){
If ($ dh = opendir ($ basedir )){
While ($ file = readdir ($ dh ))! = False ){
If ($ file! = '.' & $ File! = '..'){
If (! Is_dir ($ basedir. '/'. $ file )){
Echo 'File: '. $ 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) {
If ($ auto = 1 ){
$ Rest = substr ($ contents, 3 );
Rewrite ($ filename, $ rest );
Return ('found BOM and removed automatically ');
} Else {
Return ('find BOM ');
}
} Else {
Return ('Bom not found ');
}
}
Function rewrite ($ filename, $ data ){
$ Filenum = fopen ($ filename, 'w ');
Flock ($ filenum, LOCK_EX );
Fwrite ($ filenum, $ data );
Fclose ($ filenum );
}
?>

Code 3:

The code is as follows:


# Place the file in the directory where the BOM header needs to be removed.
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 ($ file! = '.' & $ File! = '..'){
If (! Is_dir ($ basedir. "/". $ file) {// if the file is
Echo "filename: $ basedir/$ file". checkBOM ("$ basedir/$ file ")."
";
} Else {
$ Dirname = $ basedir. "/". $ file; // if it is a directory
Checkdir ($ dirname); // recursion
}
}
}
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 of the first three characters
// The codes are
// 239
// 187
// 191
If ($ auto = 1 ){
$ Rest = substr ($ contents, 3 );
Rewrite ($ filename, $ rest );
Return ("BOM found, 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 );
}
?>


II. Python

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 ()

Bytes. Code 1: The code is as follows: function checkBOM ($ filename) {global $ auto; $ contents = fil...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.