深圳全飞鸿

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 565|回复: 1
打印 上一主题 下一主题

php处理UTF8的BOM

[复制链接]

800

主题

1379

帖子

7704

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7704
跳转到指定楼层
楼主
发表于 2021-6-13 16:21:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
$result = trim($result, "\xEF\xBB\xBF");






回复

使用道具 举报

800

主题

1379

帖子

7704

积分

版主

Rank: 7Rank: 7Rank: 7

积分
7704
沙发
 楼主| 发表于 2021-6-13 16:22:16 | 只看该作者
<?php

/*检测并清除BOM*/  

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 "filename: $basedir/$file ".checkBOM("$basedir/$file")." <br>";  

                }else{  

                    $dirname = $basedir."/".$file;  

                    checkdir($dirname);  

                }  

            }  

        }//end while  

    closedir($dh);  

    }//end if($dh  

}//end function  

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.");  

}//end function  

function rewrite($filename, $data){  

    $filenum = fopen($filename, "w");  

    flock($filenum, LOCK_EX);  

    fwrite($filenum, $data);  

    fclose($filenum);  

}//end function  

?>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|nagomes  

GMT+8, 2025-5-4 23:57 , Processed in 0.021284 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表