Home >Backend Development >PHP Tutorial >PHP implements UTF-8 file BOM automatic detection and removal examples, _PHP tutorial
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.
$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 following code as bom.php. Remember to save it in utf8 format
I hope this article will be helpful to everyone’s PHP programming design.