This paper illustrates the method of automatic detection and removal of UTF-8 file BOM in PHP. Share to everyone for your reference. The implementation methods are as follows:
BOM information is a string of hidden characters at the beginning of a file that allows some editors to recognize that this is a UTF-8 encoded file. But PHP reads these characters out of the file, creating a problem with the unrecognized characters at the beginning of the file.
For example, the use of UTF-8 format to create a picture of the PHP file, because the file header hidden BOM information is also issued, resulting in the image data is not correct, the browser is not recognized.
To detect whether a UTF-8 file contains BOM information, is the detection file at the beginning of the word three characters, whether 0xEF, 0xBB, 0xBF. Following this applet, the user traverses all the files in a directory and checks to see if the BOM is added.
Copy Code code as follows:
<?php
This file is used to quickly test UTF8 encoded files that are not BOM-added and can be removed automatically
by Bob Shen
$basedir = "."; Modifies the directory to be detected for this behavior, and the point represents the current directory
$auto = 1; Whether the found BOM information is automatically removed. 1 for Yes, 0 for No.
Don't change the following
if ($dh = Opendir ($basedir)) {
while (($file = Readdir ($DH))!== false) {
if ($file!= '. ' && $file!= ' ... ' &&!is_dir ($basedir.) /". $file)) echo" FileName: $file ". Checkbom (" $basedir/$file ")." <br> ";
}
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 ("<font Color=red>bom found, 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);
}
Save the above code as del_bom.php, and then run after modifying the directory you want to detect. This may help you detect which file contains BOM information that causes all pages to begin with a blank space.
Save the following code as bom.php remember to save as UTF8 format
Copy Code code as follows:
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<?php
This file is used to quickly test UTF8 encoded files that are not BOM-added and can be removed automatically
by Bob Boss
Wind Yin Modified
$ directory = Str_replace ("", "|", $_post["dir"]);//Accept submitted path data
$basedir = "$ directory"; Modifies the directory to be detected for this behavior, and the point represents the current directory
$auto = 1; Whether the found BOM information is automatically removed. 1 for Yes, 0 for No.
Don't change the following
if ($dh = Opendir ($basedir)) {
while (($file = Readdir ($DH))!== false) {
if ($file!= '. ' && $file!= ' ... ' &&!is_dir ($basedir.) /". $file)) echo" FileName: $file ". Checkbom (" $basedir/$file ")." <br> ";
}
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 ("<font Color=red>--bom has been cleared.") </font> ");
} else {
Return ("<font Color=red>--bom found.</font>");
}
}
else return ("--not checked to BOM.");
}
function rewrite ($filename, $data) {
$filenum =fopen ($filename, "w");
Flock ($filenum, LOCK_EX);
Fwrite ($filenum, $data);
Fclose ($filenum);
}
?>
<form action= "" method= "POST" >
Directory: <input type= "text" name= "dir"/>
<input type= "Submit" value= "Detection catalogue" >
</form>
Please enter a folder name, such as Plugin/fanfou, which does not need to be added/. If you want to detect root entries. ” . is a decimal point to commit both
</br>
I hope this article will help you with your PHP program design.