One-click removal of webpage BOM attribute [Solve garbled text, leading blank space, and verification code not displaying issues]
Published: 2019-09-29
Several frequently encountered issues:
1. The website opens blank.
2. Extra blank space appears at the top of the page.
3. The website displays garbled characters, such as “锘�”.
4. The admin login captcha does not display.
Solutions:
1. Use a professional editor, such as Notepad++, Sublime, or EditPlus; these will not automatically add a BOM signature.
2. In Sublime, go to File -> Save with Encoding -> UTF-8. After saving, the BOM can be removed.
3. In Notepad++, select Format -> Encode in UTF-8 to remove it.
4. Use the following method to remove the BOM from all text files in the website in one go.
Copy the following code
<?php
$dir=dirname(__FILE__);
if(isset($_GET["act"]) && $_GET["act"] == "del" ){ //Del the file!!
$filename=basename($_SERVER['PHP_SELF']);
@unlink($filename);
exit('Error:File Not Found!');
}
echo "The document:".$dir." <a href=\"?act=del\" >[Del the delbom.php file]</a><BR/>";
if (!is_dir($dir))
die("$dir not exists");
$auto = 1;
checkdir($dir);
function checkdir($basedir){
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..'){
if (!is_dir($basedir."/".$file)) {
echo "filename: $basedir/$file ".checkBOM("$basedir/$file")."<BR/>\n";
}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 ("<font color=red> BOM found, automatically removed.</font>\n");
} else {
return ("<font color=red>BOM found.</font>\n");
}
}
else return ("BOM Not Found.\n");
}
function rewrite ($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>
Copy the above code Usage: Copy the above code into a newly created delbom.php file (open this file with Notepad or another text editor), upload it to the root directory of your website, then visit your site, for example http://www.**.com/delbom.php to clean all BOM tags present in the web pages.
