Home Backend Development PHP Tutorial PHP code example for batch deletion and clearing UTF-8 file BOM header_PHP tutorial

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

Jul 13, 2016 am 10:32 AM
php

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 ("<font color=red>BOM found, automatically removed.</font>");
} else {
return ("&lt ;font color=red>BOM found.</font>");
                                                                                                                                            🎜>

Code 2:



Copy code

The code is as follows:


<?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 '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<br />': 'Only detect file BOM and do not perform BOM removal<br />';

checkdir($basedir);
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

The code is as follows:


##把该文件放在需求去除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()

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/754341.htmlTechArticle记得运行代码前先把文件备份一下哦,避免出现失败问题。 代码一: 复制代码 代码如下: function checkBOM ($filename) { global $auto; $contents = fil...
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles