php bom是什么意思

学习笔记 03/28 PHP

php bom是用来判断文本文件是哪一种unicode编码的标记;在utf-8编码文件中,bom在文件头部,占用三个字节,用来标示该文件属于utf-8编码。

php bom是什么意思

本教程操作环境:Windows10系统、PHP8.1版、DELL G3电脑

php bom是什么意思?

PHP-BOM

1.问题描述:所有使用控制层返回的ajax请求都会在前面附加一个小红点,查了一下是因为文件前面有BOM,虽然事后证明并不是所有的文件都带有BOM,但是却造成所有的文件返回数据都出现问题。

2.那么,什么是BOM呢?

答曰:在utf-8编码文件中BOM在文件头部,占用三个字节,用来标示该文件属于utf-8编码,现在已经有很多软件识别bom头,但是还有些不能识别bom头,比如PHP就不能识别bom头,这也是用记事本编辑utf-8编码后执行就会出错的原因了。

3.解决方案,去网上找一些批量处理BOM的脚本运行一下就行了,你也可以运行像editplus、Ultraedit这样的工具来解决。

4.最后提供一个去BOM的文件,运行一下就可以了。

echo '当前查找的目录为:'.$basedir.'当前的设置是:';
echo $auto?&#39;检测文件BOM同时去除检测到BOM文件的BOM<br />&#39;:&#39;只检测文件BOM不执行去除BOM操作<br />&#39;;
checkdir($basedir);
function checkdir($basedir){
    if($dh=opendir($basedir)){
        while (($file=readdir($dh)) !== false){
            if($file != &#39;.&#39; && $file != &#39;..&#39;){
                if(!is_dir($basedir.&#39;/&#39;.$file)){
                    echo &#39;文件: &#39;.$basedir.&#39;/&#39;.$file .checkBOM($basedir.&#39;/&#39;.$file).&#39; <br>&#39;;
                }else{
                    $dirname=$basedir.&#39;/&#39;.$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 (&#39; <font color=red>找到BOM并已自动去除</font>&#39;);
        }else{
            return (&#39; <font color=red>找到BOM</font>&#39;);
        }
    }else{
        return (&#39; 没有找到BOM&#39;);
    }
}
function rewrite($filename,$data){
    $filenum=fopen($filename,&#39;w&#39;);
    flock($filenum,LOCK_EX);
    fwrite($filenum,$data);
    fclose($filenum);
}
?>

推荐学习:《》

以上就是php bom是什么意思的详细内容,更多请关注本站点其它相关文章!

本文地址:https://www.stayed.cn/item/27476

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。