In a UTF-8 encoded file, the BOM is in the header of the file, occupying three bytes, which is used to indicate that the file belongs to UTF-8 encoding. There are many softwares that recognize the BOM header, but there are still some that cannot recognize the BOM header. For example, PHP cannot recognize the BOM header, which is why an error occurs after editing UTF-8 encoding with Notepad. The BOM is only available when "Notepad" is used to store it as UTF-8 under Windows. You can use WINHEX to delete the first 2 bytes. In the encoding settings in Dreamweaver, you can set whether to include BOM. Generally, as long as the output of PHP is not a picture (GDI
Stream), BOM will not cause problems. GDI Stream will be displayed as a red cross if there are extra characters at the beginning.
There are two simple ways to remove bom:
1. How to remove BOM header in editplus
After the editor is adjusted to the UTF8 encoding format, there will be a string of hidden characters (that is, BOM) in front of the saved file, which is used by the editor to identify whether the file is UTF8 encoded. Run Editplus, click Tools, select Preferences, select the file, and select UTF-8 identification. Always delete the signature, and then edit and save the PHP file. The PHP file will not have a BOM.
2. Ultraedit method to remove bom
After opening the file, select the encoding format in the Save As option (utf-8 without BOM header), and confirm.
For PHP programs that need to remove the Bom header, you can also use the bom.php program in the directory to remove it.
<?php
/*清除rom*/
if(isset($_GET['dir'])){
$basedir=$_GET['dir'];
}else{
$basedir = '.';
}
$auto = 1;
checkdir($basedir);
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>";
}else{
$dirname = $basedir."/".$file;
checkdir($dirname);
}
}
}//end while
closedir($dh);
}//end if($dh
}//end function
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.");
}//end function
function rewrite($filename, $data){
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>
http://www.bkjia.com/PHPjc/755769.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755769.htmlTechArticleIn a utf-8 encoded file, the BOM is in the header of the file and occupies three bytes to mark the file. It belongs to UTF-8 encoding. There are many softwares that can recognize the BOM header, but there are still some that cannot recognize the BOM header...
Statement: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