用PHP解决万恶的BOM_PHP教程
$path = dirname(__FILE__);
scandDir($path);
function bomtoutf8($filename)
{
$ef = 239;
$bb = 187;
$bf = 191;
if (is_dir($filename)) return;
$fd = fopen($filename, "rb");
if (!$fd) exit($filename);
$headers = fread($fd, 3);
fclose($fd);
if (strlen($headers)
if (ord($headers[0]) == $ef &&
ord($headers[1]) == $bb &&
ord($headers[2]) == $bf
) {
echo $filename, "\n";
$content = file_get_contents($filename);
if (!$content) {
exit("file get contents failed");
}
$len = strlen($content);
$content = substr($content, 3);
if ($content === false && $len!=3) {
exit("sbustr failed");
} else if ($content === false && $len==3){
$content = "";
}
$ret = file_put_contents($filename, $content, LOCK_EX);
if ($ret === false) {
exit("file put contents failed");
}
echo "successed file ", $filename, "\n";
}
}
function scandDir($path)
{
$d = dir($path);
if (!$d) exit("dir failed");
while (false !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$filename = $path . DIRECTORY_SEPARATOR . $entry;
// echo $filename, "\n";
if (is_dir($filename)) {
scandDir($filename);
} else {
bomtoutf8($filename);
}
}
}
$path = dirname(__FILE__);
scandDir($path);
function bomtoutf8($filename)
{
$ef = 239;
$bb = 187;
$bf = 191;
if (is_dir($filename)) return;
$fd = fopen($filename, "rb");
if (!$fd) exit($filename);
$headers = fread($fd, 3);
fclose($fd);
if (strlen($headers)
if (ord($headers[0]) == $ef &&
ord($headers[1]) == $bb &&
ord($headers[2]) == $bf
) {
echo $filename, "\n";
$content = file_get_contents($filename);
if (!$content) {
exit("file get contents failed");
}
$len = strlen($content);
$content = substr($content, 3);
if ($content === false && $len!=3) {
exit("sbustr failed");
} else if ($content === false && $len==3){
$content = "";
}
$ret = file_put_contents($filename, $content, LOCK_EX);
if ($ret === false) {
exit("file put contents failed");
}
echo "successed file ", $filename, "\n";
}
}
function scandDir($path)
{
$d = dir($path);
if (!$d) exit("dir failed");
while (false !== ($entry = $d->read())) {
if ($entry == '.' || $entry == '..') {
continue;
}
$filename = $path . DIRECTORY_SEPARATOR . $entry;
// echo $filename, "\n";
if (is_dir($filename)) {
scandDir($filename);
} else {
bomtoutf8($filename);
}
}
}
友情提示:代码是批量处理目录下(包括子目录)的所有带BOM文件,使用前需备份
上一篇: PHP中IP地址转真实地址
下一篇: PHP基础教程十二之抽象、接口
推荐阅读
-
用PHP实现的生成静态HTML速度快类库_PHP教程
-
php采集时被封ip的解决方法_PHP教程
-
url传递中文字符,特殊危险字符的解决方案(仅供参考)urldecode、base64_encode_PHP教程
-
解决php中Cannot send session cache limiter 的问题的方法_PHP教程
-
php 环境本地用ip连接mysql响应快,用localhost连接慢的解决
-
IE6通过get发送奇数个汉字请求会乱码的解决方法_PHP教程
-
ThinkPHP中SHOW_RUN_TIME不能正常显示运行时间的解决方法 原创,thinkphprun_PHP教程
-
php history.back返回后表单数据丢失的解决办法,history.back_PHP教程
-
fleaphp下不确定的多条件查询的巧妙解决方法_PHP教程
-
BS模式中用php创建socket 无权限的解决办法_PHP教程