去掉php bom的方法:1、找到php根目录;2、将“function checkbom($filename){…}”等代码放到根目录下,并通过浏览器运行访问即可。
本文操作环境:windows7系统,php7.4版,dell g3电脑。
php bom怎么去掉?
php去除bom简单的方法
/* ——————————————————————————————- title : 去掉bom头方法 author : hello_sgw version : v1.0.0.1 initial-time : 2017-08-12 15:18 last-time : 2017-08-12 16:01 desc : ——————————————————————————————- */
自己在调用接口时候,因为用到了对方提供的封装方法,在输出一组数据时候一直显示错误,最后想到可能对方给的方法里面含有编码问题(具有bom头),所以上网搜索到一个检测bom的方法并且可以去除重新生成新文件,运用之后就能正常显示数据了。
什么是bom头?
bom –byte order mark,中文名译作“字节顺序标记”,在utf-8编码文件中bom在文件头部,占用三个字节,用来标示该文件属于utf-8编码,
现在已经有很多软件识别bom头,但是还有些不能识别bom头,比如php就不能识别bom头,这也是用记事本编辑utf-8编码后执行就会出错的原因了。
解决方法:
# 这里代码为php方式去除当前目录及字目录所有文件bom信息,只要将此代码文件放到根目录下,然后浏览器运行访问就可以了<?phpif (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); } } } 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 found, automatically removed.</font>\”); } else { return (\”<font color=\’red\’>bom found.</font>\”); } } else return (\”bom not found.\”);}function rewrite($filename, $data){ $filenum = fopen($filename, \”w\”); flock($filenum, lock_ex); fwrite($filenum, $data); fclose($filenum);}
推荐学习:《php视频教程》
苹果点删除还是卸载好_苹果手机卸载app和删除app的区别图文步骤嘉兴网站建设网站建设需要注意什么?建设网站需要多少钱?B端网站关键词选的好,精准流量没得跑!云服务器网站打不开怎么办半年时间,NFT.APP域名获利百倍Blender人物建模面部模型创建 Blender人物头部建模创建方法阿里云服务器和万网的区别263企业邮箱是网易的吗