作为一名 PHP 程序员,你经常会遇到浏览器样式混乱的问题。 当然php空白,这并不是指csshack问题。 在chrome下正常,但在ie类型内核浏览器上不正常。
很多时候你在查看网页源代码时会看到远处前面有一个换行符。 您可以检查所有加载的 PHP 代码都没有相关的换行符。 通过复制源代码并用二进制文件打开,腹中会有 EFBBBF 内容,好吧,不幸的是告诉你,你使用的编辑器手动在你的文件中添加了 utf-8 BOM
当然,上面只是一种情况,还有一些比如腹中session_start()之前的输出,关闭服务器时会出现空行并报错。
一般来说,腹部空白有以下三个原因: 什么是BOM?
“EF BB BF”这三个字节称为BOM,BOM的全称称为“Byte Order Mard”。 BOM经常用在utf-8文件中来表明该文件是UTF-8文件,而BOM的本意是在utf16中用来表示高低字节的顺序。 字节流之前有一个BOM表示使用低字节序列(低字节在上面),而utf8不需要考虑字节序列php空白,所以虽然没有BOM。 UTF-8使用字节作为编码单位,不存在字节顺序问题。 UTF-16使用两个字节作为编码单元。 在解释UTF-16文本之前,必须首先弄清楚每个编码单元的字节顺序。 例如“Kui”的Unicode编码是594E,“B”的Unicode编码是4E59。 如果我们收到UTF-16字节流“594E”,这是“Kui”还是“B”?
最后提供一个utf8bom回来的PHP类
<?php
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")." nr";
}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 ("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);
}
参考字符串说明了解UTF8和UTF8BOM的区别