在Joomla!模块文件中,如果有模板文件含有BOM头,有时会导致页面乱码。有时在IE下会莫名其妙的出一行空白,而在firefox下会出现无法显示的字符。在linux下查找当前目录以及子目录中所有有BOM头的文件过程如下:
1、在document root下创建一个文件 check.php,代码如下:
<?php
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 ("");
}
$filename=$argv[1];
echo $filename;
echo checkBOM($filename);
echo "\n"
?>
2、在document root下运行:
find . -name "*.php" -exec /usr/local/php/bin/php check.php {} \; | grep BOM
这样就可以找到所有含有BOM的文件,将BOM去掉就可以了。