How to remove bom from php code

05-16-2024

Ways to remove bom from php code: 1. Remove the bom header in the text by using function clear BOM ($ contents) {...}; 2. Detect and remove the bom header by the function check BOM ($ filename) {...} method; 3. Use the function search BOM ($ string) {...} method to search whether there is a BOM in the current file and remove it.

Php introduction to employment online live class: enter the learning API post = postman+swagger+mock+jmeter super easy-to-use API debugging tool: click to use.

Operating environment of this tutorial: windows7 system, PHP version 8.1 and Dell G3 computer.

How to remove bom from php code?

PHP removes bom headers from utf8 format files in batches.

I often use txt text editor to write php files, so I often add bom headers automatically, which will lead to problems in many cases, such as our session can't work, cookie can't be set and so on.

Below, I have compiled several methods to clear the bom header in utf8 format files by using php programs.

Example 1

The code is as follows

/** * remove the bom header in the file. * @var 0.1 * @author Chenwp */ function clearbom($contents){ //UTF8 removes the bom header from the text. $BOM = chr(239).chr(187).chr(191); return str_replace($BOM,,$contents); } /** * remove the bom header in the file. * @param object $fileName Description * @return object Description */ function clearfilebom($fileName){ $c = file_get_contents($fileName); $c = clearbom($c); file_put_contents($fileName,$c); }

Example 2

How to convert the format with BOM file into unsigned UTF-8 file? Let's share with you a PHP code:

The code is as follows

Example 3

All subdirectories and files are automatically scanned.

The code is as follows

UTF8 BOM cleaner body { font-size: 10px; font-family: Arial, Helvetica, sans-serif; background: #FFF; color: #000; } .FOUND { color: #F30; font-size: 14px; font-weight: bold; }

Recommended study: PHP video tutorial

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us