How to remove php bom

藏色散人
Release: 2023-03-14 11:00:02
Original
1594 people have browsed it

How to remove php bom: 1. Find the PHP root directory; 2. Put the "function checkBOM($filename){...}" and other codes in the root directory, and run it through the browser to access it. Can.

How to remove php bom

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

How to remove php bom?

Simple method to remove BOM in PHP

/* 
 +------------------------------------------------------------------------------------------- 
 + 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         : 
 +------------------------------------------------------------------------------------------- 
*/
Copy after login

When I called the interface, because I used the encapsulation method provided by the other party, an error kept displaying when outputting a set of data. Finally, I thought of the possibility The method given by the other party contained coding issues (with BOM header), so I searched online for a method to detect BOM and could remove it and regenerate a new file. After using it, the data can be displayed normally.

What is the BOM header?

BOM --Byte Order Mark, the Chinese name is translated as "byte order mark". In a utf-8 encoded file, the BOM is at the head of the file and occupies three bytes to mark the file. It belongs to utf-8 encoding.

Now there are many softwares that recognize BOM header, but there are still some that cannot recognize BOM header. For example, PHP cannot recognize BOM header. This is also done after editing the UTF-8 encoding with Notepad. The reason why something goes wrong.

Solution:

# 这里代码为PHP方式去除当前目录及字目录所有文件BOM信息,只要将此代码文件放到根目录下,然后浏览器运行访问就可以了
<?php
if (isset($_GET[&#39;dir&#39;])) { //设置文件目录 
  $basedir = $_GET[&#39;dir&#39;];
} else {
  $basedir = &#39;.&#39;;
}
 
$auto = 1;
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 . "/" . $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=&#39;red&#39;>BOM found, automatically removed.</font>");
    } else {
      return ("<font color=&#39;red&#39;>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);
}
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove php bom. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
bom
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!