引用文件指的是include、require_once引用其它文件引起的空白行,这其实是你用记事本编辑utf-8文件以后,虽然你还是保存为utf-8格式,但系统会自动给文件加两个bom标记,但是ie只能忽略一个,另一个就是空白行了。
解决办法就是:不要用记事本去编辑UTF-8文件,要用其它工具,比Dreamw、EmEditor 、Visual Studio 2008等编辑工具编辑UTF-8文件后保存。
我的两个站用记事本编辑后顶部就有空白行,起初以为是css设置问题,结果是bom标记引起的。
还有一种办法,如果你是大量文件都这样我们可使用一个php函数来搞定,把以下代码复制到一个PHP文件里面,然后放在需要清除BOM头的目录一下运行,就可以批量把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") . " <br>";
} 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>");
} else {
return ("<font color=red>BOM found.</font>");
}
} else return ("BOM Not Found.");
}
function rewrite($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>立即学习“PHP免费学习笔记(深入)”;
永久链接:
转载随意!带上文章地址吧。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号