PHP bulk Delete, purge UTF-8 file BOM Header code example _php tutorial

Source: Internet
Author: User
Tags flock
Remember to back up the files before running the code oh, to avoid failure problems.

Code One:
Copy CodeThe 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 two:
Copy the Code code as follows:
Header (' content-type:text/html; Charset=utf-8 ');
if (Isset ($_get[' dir '))) {//Set file directory, if not set, automatically set to the directory where the current file is located
$basedir =$_get[' dir '];
}else{
$basedir = '. ';
}
$auto =1;/* is set to 1 mark to detect BOM and remove, set to 0 mark only for BOM detection, do not remove */

Echo ' Currently looking for directory is: '. $basedir. ' The current setting is: ';
echo $auto? ' Detects the BOM of a BOM while removing the bill of documentation
': ' Only detect file BOM does not perform removal of BOM operation
';

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 (' Find BOM and automatically remove ');
}else{
Return (' Find BOM ');
}
}else{
Return (' No BOM found ');
}
}
function rewrite ($filename, $data) {
$filenum =fopen ($filename, ' w ');
Flock ($filenum, LOCK_EX);
Fwrite ($filenum, $data);
Fclose ($filenum);
}
?>

Code Three:
Copy the Code code 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 ($file! = '. ' && $file! = ' ... ') {
if (! Is_dir ($basedir. "/" . $file)) {//If it is a file
echo "FileName: $basedir/$file". Checkbom ("$basedir/$file"). "
";
} else {
$dirname = $basedir. "/" . $file; If the directory
Checkdir ($dirname); Recursive
}
}
}
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
The first three characters of the ASCII
The codes were
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);
}
?>


Second, Python
Copy the Code code 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) has a BOM." Deleted. "

if __name__ = = "__main__":
Delbom ()

http://www.bkjia.com/PHPjc/754341.html www.bkjia.com true http://www.bkjia.com/PHPjc/754341.html techarticle remember to back up the files before running the code oh, to avoid failure problems. Code One: Copy the code 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.