Home >Backend Development >PHP Problem >How to remove php 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.
#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 : +------------------------------------------------------------------------------------------- */
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['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); }
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!