腾讯云服务器优惠活动华纳云香港服务器

导致页面空白一行的&#65279如何去掉?

PHP 会输出空白隐形字符 &#65279,这样一来就会导致页面出现空白一行的情况,具体情况见下图:

导致页面空白一行的&#65279如何去掉?-第1张-boke112百科(boke112.com)

这种因为 &#65279 导致页面空白一行的解决办法也很简单,只需要找到相应的 PHP 文件,然后用编辑器打开该文件然后另存为 UTF-8 无 BOM 格式即可。如 Notepad++ 软件,打开该文件后,点击菜单栏的编码 – 选择“以 UTF-8 无 BOM 格式编码”,然后保存即可。

导致页面空白一行的&#65279如何去掉?-第2张-boke112百科(boke112.com)

PS:window 的记事本保存的文件都是带 BOM 的,所以编辑 PHP 文件不建议使用记事本,切记!!!

当然,你也可以将以下代码另存为 PHP 文件,上传到站点根目录下运行,就会把目录下全部 utf-8 文件的 bom 头去掉。

  1. //remove the utf-8 boms  
  2. //by magicbug at gmail dot com  
  3. if (isset($_GET[‘dir’])){ //config the basedir  
  4. $basedir=$_GET[‘dir’];
  5. }else{
  6. $basedir = ‘.’;
  7. }
  8. $auto = 1;
  9. checkdir($basedir);
  10. function checkdir($basedir){
  11. if ($dh = opendir($basedir)) {
  12. while (($file = readdir($dh)) !== false) {
  13. if ($file != ‘.’ && $file != ‘..’){
  14. if (!is_dir($basedir.“/”.$file)) {
  15. echo “filename
  16. $basedir/$file “.checkBOM(“$basedir/$file”).” <br>”;
  17.                  }else{
  18. $dirname = $basedir.“/”.$file;
  19.                      checkdir($dirname);
  20.                  }
  21.              }
  22.          }
  23.      closedir($dh);
  24.      }
  25. }
  26. function checkBOM ($filename) {
  27. global $auto;
  28. $contents = file_get_contents($filename);
  29. $charset[1] = substr($contents, 0, 1);
  30. $charset[2] = substr($contents, 1, 1);
  31. $charset[3] = substr($contents, 2, 1);
  32. if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
  33. if ($auto == 1) {
  34. $rest = substr($contents, 3);
  35.              rewrite ($filename$rest);
  36. return (“<font color=red>BOM found, automatically removed.</font>”);
  37.          } else {
  38. return (“<font color=red>BOM found.</font>”);
  39.          }
  40.      }
  41. else return (“BOM Not Found.”);
  42. }
  43. function rewrite ($filename$data) {
  44. $filenum = fopen($filename“w”);
  45.      flock($filenum, LOCK_EX);
  46.      fwrite($filenum$data);
  47.      fclose($filenum);
  48. }
何谓 BOM?

 UTF-8 编码的文件可以分为无 BOM 和 BOM 两种格式。

“EF BB BF” 这三个字节就叫 BOM,全称是”Byte Order Mard”。在 utf8 文件中常用 BOM 来表明这个文件是 UTF-8 文件,而 BOM 的本意是在 utf16 中用。utf-8 文件在 php 中输出的时候 bom 是会被输出的,所以要在 php 中使用 utf-8,必须要使用不带 bom 头的 utf-8 文件。

拓展阅读

本文地址:https://boke112.com/post/4126.html

赞 (0) 打赏
版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu