PHP implements examples of automatic detection and removal of UTF-8 file BOM,
The example in this article describes how to implement automatic detection and removal of UTF-8 file BOM in PHP. Share it with everyone for your reference. The specific implementation method is as follows:
BOM information is a string of hidden characters at the beginning of the file, which is used by some editors to recognize that this is a UTF-8 encoded file. However, PHP will read out these characters when reading the file, causing the problem that the beginning of the file contains some unrecognizable characters.
For example, if a PHP file that generates an image is saved in UTF-8 format, because the BOM information hidden in the file header is also distributed, the generated image data is incorrect and cannot be recognized by the browser.
To detect whether a UTF-8 file contains BOM information, it is to detect whether the three characters at the beginning of the file are 0xEF, 0xBB, 0xBF. In the following small program, the user traverses all files in a certain directory and detects whether a BOM has been added.
Copy code The code is as follows:
//This file is used to quickly test whether the UTF8 encoded file has a BOM added and can be automatically removed
//By Bob Shen
$basedir="."; //Modify the directory that needs to be detected for this behavior. The dot indicates the current directory
$auto=1; //Whether to automatically remove the discovered BOM information. 1 means yes, 0 means no.
//No need to 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" )."
";
}
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 ("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);
}
Save the above code as del_bom.php, modify the directory to be detected and run it. This may help detect which file contains BOM information that causes a blank space at the beginning of all pages.
Save the following code as bom.php. Remember to save it in utf8 format
Copy code The code is as follows:
Detection results in the directory
//This file is used to quickly test whether the UTF8 encoded file has a BOM added and can be automatically removed
//By bob boss
//Fengyin modification
$directory = str_replace(" ", "|", $_POST["dir"]);//Accept the submitted path data
$basedir="$directory"; //Modify the directory that needs to be detected for this behavior. The dot indicates the current directory
$auto=1; //Whether to automatically remove the discovered BOM information. 1 means yes, 0 means no.
//No need to 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" )."
";
}
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 ("
--Bom has been cleared.");
} else {
return ("
--Bom found.");
}
}
else return ("--No Bom.");
}
function rewrite ($filename, $data) {
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?>
Please enter the folder name such as plugin/fanfou. There is no need to add / after it. If you want to detect the root directory, enter "." . is a decimal point and you can submit
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/906667.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906667.htmlTechArticlePHP implements UTF-8 file BOM automatic detection and removal examples. This article describes the PHP implementation of UTF-8 files. Method for automatically detecting and removing BOM. Share it with everyone for your reference. Specific implementation method...