用php把utf8的中的BOM去掉
2010-04-01 09:19
435 查看
今天看到一段不错的php程序,可以自动把你所有的文件的BOM去掉,而这全部过程只需要上传并运行一下,非常不错,保留下来方便下次使用。
代码
Write UTF-8 BOM=0
Write UTF-8 BOM NF=0
这段php程序如下,保存成php文件放在根目录中执行一次就行了,会自动去除文件头中的BOM。
<?php
//remove the utf-8 boms
//by magicbug at gmail dot com
if (isset($_GET['dir'])){ //config the basedir
$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);
}
?>
代码
Write UTF-8 BOM=0
Write UTF-8 BOM NF=0
这段php程序如下,保存成php文件放在根目录中执行一次就行了,会自动去除文件头中的BOM。
<?php
//remove the utf-8 boms
//by magicbug at gmail dot com
if (isset($_GET['dir'])){ //config the basedir
$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中出现BOM字符\ufeff,PHP去掉诡异的BOM \ufeff
- 去掉utf8 bom
- PHP去掉诡异的BOM \ufeff
- 快速测试UTF8编码的文件是不是加了BOM,不限PHP
- PHP 下载文件时如何自动添加bom头及解释BOM头和去掉bom头的方法
- PHP中出现BOM字符\ufeff,PHP去掉诡异的BOM \ufeff
- 用PHP去掉文件头的Unicode签名(BOM)方法
- PHP批量去掉诡异的BOM \ufeff,消灭莫名其妙的小红点
- 递归删除utf8文件的bom头(该bom头可能导致php产生意外输出)
- php文件存储时的编码格式--utf8无BOM
- PHP自动去掉BOM
- php和bom头的冤仇 和 如何在linux下使用命令去掉bom头
- 去掉utf8 bom (找了好久才找到) 防止UTF-8页面上传服务器, 出现错位之类的错误
- 用PHP去掉文件头的Unicode签名(BOM)
- PHP 去掉UTF-8+BOM 文件字节前缀
- PHP 下载文件时如何自动添加bom头及解释BOM头和去掉bom头的方法
- php递归检查是否含有utf8 bom
- php头部空行|utf8头部空行|关于bom以及utf-8的问题
- php中utf8 与utf-8 与utf8 无BOM
- 递归删除utf8文件的bom头(该bom头可能导致php产生意外输出)