Joomla!-开源天空

2009-01-07
您所在的位置: 首页 > Joomla专栏 > 应用 > linux下如何查找当前目录中含有BOM的文件

linux下如何查找当前目录中含有BOM的文件

Joomla! 开源天空  作者:管理员  2008-09-14 18:03
  • 摘要:本文描述了如何查找当前目录以及子目录下含有BOM头的文件,并给出代码示例。

在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去掉就可以了。

  发表您的文章评论

您的姓名 (昵称)
标题:
评分: 很差一般较好很好
评论:
验证码:
请输入验证码