php bom怎么去掉
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信息,只要将此代码文件放到根目录下,然后浏览器运行访问就可以了 <?php 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); } } } 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); }
1. 本站所有资源来源于用户分享和网络转载,如有侵权请联系站长删除!
2. 分享目的仅供大家学习参考,源码类您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
917资源网 » php bom怎么去掉
2. 分享目的仅供大家学习参考,源码类您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
917资源网 » php bom怎么去掉