Php批量去除bom头信息的实现代码
发布时间:2017-02-14 编辑:jiaochengji.com
分享一例批量去除bom头信息的代码,om让人很头疼,在php开发中产生乱码致使很多问题发生,这里分享个批量去除工具,有需要的朋友参考下。
一个不错的批量去除bom头信息的工具,避免产生一些乱码问题。
例子:
复制代码 代码示例:
<?php
/**
* 去除bom头信息
* edit: www.jbxue.com
*/
header("Content-Type:text/html; charset=utf-8");
if (isset($_GET['dir'])){ //设置文件目录
$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 "文件名称: $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并且已自动删除</font>");
} else {
return ("<font color=red>发现BOM</font>");
}
}
else return ("没有发现BOM");
}
function rewrite ($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>
/**
* 去除bom头信息
* edit: www.jbxue.com
*/
header("Content-Type:text/html; charset=utf-8");
if (isset($_GET['dir'])){ //设置文件目录
$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 "文件名称: $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并且已自动删除</font>");
} else {
return ("<font color=red>发现BOM</font>");
}
}
else return ("没有发现BOM");
}
function rewrite ($filename, $data) {
$filenum = fopen($filename, "w");
flock($filenum, LOCK_EX);
fwrite($filenum, $data);
fclose($filenum);
}
?>
您可能感兴趣的文章:
PHP实例:检测并清除文件开头的BOM信息
php去掉bom头的代码分享
PHP 过滤页面中的BOM数据的简单实例
检测php文件是否有bom头的代码
批量清除php文件中bom的方法
检查并清除php文件中bom的函数
有关 UTF-8 BOM 导致样式错乱的解决方法
BOM与DOM的区别分析
有关UTF-8 编码中BOM的检测与删除
您可能感兴趣的文章:
PHP 过滤页面中的BOM数据的简单实例
php去掉bom头的代码分享
Php批量去除bom头信息的实现代码
PHP实例:检测并清除文件开头的BOM信息
UTF-8编码怎么去掉BOM头?
PHP批量检测并去除文件BOM头信息代码
php批量删除、清除utf-8文件bom头的代码
PHP如何批量检测并去除文件BOM头
批量清除php文件中bom的方法
php中curl实现GET和POST请求抓取网页或上传文件
上一篇:Php删除指定文件与文件夹的方法
下一篇:PHP 取整函数参考
[关闭]