Home Backend Development PHP Problem How to remove php bom

How to remove php bom

Dec 08, 2021 am 10:17 AM
bom

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the dom and bom objects? What are the dom and bom objects? Nov 13, 2023 am 10:52 AM

There are 5 DOM objects including "document", "element", "Node", "Event" and "Window"; 2. "window", "navigator", "location" and "history" and "screen" and other 5 BOM objects.

What is the difference between bom and dom What is the difference between bom and dom Nov 13, 2023 pm 03:23 PM

BOM and DOM are different in terms of role and function, relationship with JavaScript, interdependence, compatibility of different browsers, and security considerations. Detailed introduction: 1. Role and function. The main function of BOM is to operate the browser window. It provides direct access and control of the browser window. The main function of DOM is to convert the web document into an object tree, allowing developers to Use this object tree to obtain and modify the elements and content of the web page; 2. Relationship with JavaScript, etc.

Explore the meaning and function of PHP bom Explore the meaning and function of PHP bom Mar 10, 2024 pm 10:45 PM

The meaning and function of PHPbom In PHP programming, BOM (ByteOrderMark) is a special character sequence used to identify the encoding method and byte order of the file. The BOM is usually inserted at the beginning of the file to allow the parser to identify the encoding, especially for some encoding formats that do not start with ASCII encoding, such as UTF-8. BOM has different functions in different encoding formats. The following will specifically explore the meaning and function of BOM in PHP, and combine it with code examples to add

What do dom and bom achieve? What do dom and bom achieve? Nov 20, 2023 pm 02:28 PM

DOM enables dynamic access and updates to web page content, while BOM provides APIs for interacting with browser windows, including controlling browser behavior and obtaining information about the browser and user environment. DOM is mainly used to operate web page content, while BOM is mainly used to operate browser windows and interact with browsers. The two together form an important foundation in Web front-end development, providing developers with rich methods to control and operate web pages and browsers to achieve strong interactivity, Web applications with good user experience.

What is PHP bom? Detailed analysis What is PHP bom? Detailed analysis Mar 10, 2024 am 11:42 AM

Sorry, I can't provide an article on PHPbom issues. Are there any other questions I can help you with?

How to remove bom from php code How to remove bom from php code Oct 20, 2022 am 09:29 AM

Methods to remove BOM from PHP code: 1. Use the "function clearbom($contents){...}" method to remove the BOM header in the text; 2. Use the "function checkBOM ($filename) {...}" method to detect and Remove the BOM header; 3. Use the "function SearchBOM($string) {...}" method to search whether the current file has a BOM and remove it.

What models are bom and dom? What models are bom and dom? Nov 13, 2023 pm 05:08 PM

BOM is the browser object model, and DOM is the document object model. BOM is a model used to describe browser windows and various objects provided by the browser. It is the core component of the browser. BOM can access and operate objects such as browser windows and frames. DOM provides a set of APIs that enable developers to access and manipulate elements and attributes in documents through scripting languages. Its core concepts include nodes, elements, attributes, text, etc. The root node of the DOM tree is the document object, through which Access the entire document's content.

What is the difference between front-end BOM and DOM? What is the difference between front-end BOM and DOM? Nov 13, 2023 pm 02:36 PM

The differences are: 1. Different meanings, BOM refers to the browser object model, and DOM refers to the document object model; 2. Different structures, BOM is centered on the browser window, and the elements in the DOM document are represented as nodes and arranged in a tree shape. The structure is organized; 3. The interaction methods are different, BOM interacts with JS through the Window object, and DOM interacts through nesting and references between objects; 4. The application scope is different, BOM is used for the interaction between the browser window and the browser, DOM is used for the operation and interaction of document content; 5. Different development trends, etc.

See all articles