php ufeff怎么去掉
2022-12-02 09:01:16
126
{{single.collect_count}}

php去掉ufeff的方法:1、将PHP文件保存为无dom格式;2、通过“function checkBOM($filename){...}”方法检查并去掉ufeff即可。

php入门到就业线上直播课:进入学习
Apipost = Postman + Swagger + Mock + Jmeter 超好用的API调试工具:点击使用

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑

php ufeff怎么去掉?

php 去除文件的BOM\ufeff

今天遇到一个问题,请求接口返回的数据不符合格式,用浏览器打印出来又看不出什么问题,最后发现是数据头部有\UFEFF(BOM头),搜索下,是文件编码格式为UTF8-BOM引起的,开发时编辑器保存格式有误,解决办法有两种:

1、文件量少,可以用notopad++ 保存为无dom格式(格式->转为UTF-8 无dom格式)

2、文件很多时,用下列代码,运行后可以对指定目录(不传参则为当前目录)下所有文件进行修复

<?phpheader('content-Type: text/html; charset=utf-8');if(isset($_GET['dir'])){ //设置文件目录,如果没有设置,则自动设置为当前文件所在目录$basedir=$_GET['dir'];}else{$basedir='.';}$auto=1;/*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/echo '当前查找的目录为:'.$basedir.'当前的设置是:';echo $auto?'检测文件BOM同时去除检测到BOM文件的BOM<br />':'只检测文件BOM不执行去除BOM操作<br />';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视频教程

以上就是php ufeff怎么去掉的详细内容,更多请关注php中文网其它相关文章!

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}