本文主要为大家带来一篇用php去掉文件头的unicode签名(bom)方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能你帮助到大家。
废话不多说,直接上代码
<?php
//此文件用于快速测试utf8编码的文件是不是加了bom,并可自动移除
//by bob shen
$basedir="."; //修改此行为需要检测的目录,点表示当前目录
$auto=1; //是否自动移除发现的bom信息。1为是,0为否。
//以下不用改动
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file!='.' && $file!='..' && !is_dir($basedir."/".$file)) echo "filename: $file ".checkbom("$basedir/$file")." <br>";
}
closedir($dh);
}
function checkbom ($filename) {
$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) {
$rest=substr($contents, 3);
rewrite ($filename, $rest);
return true;
}
else return ("bom not found.");
}
function rewrite ($filename, $data) {
$filenum=fopen($filename,"w");
flock($filenum,lock_ex);
fwrite($filenum,$data);
fclose($filenum);
}
?>
相关推荐:
php中json_encode中文unicode转码的问题解决
css中关于direction与unicode-bidi的对比
分享一个用php将unicode 转化为utf-8的实现方法
以上就是php去掉文件头的unicode签名的方法的详细内容。