Home > Backend Development > PHP Problem > How to remove php bom

How to remove php bom

藏色散人
Release: 2023-03-02 13:18:01
Original
2696 people have browsed it

Method to remove php BOM: First use the fopen function to open the php file; then read the content of the php file; then call ltrim to remove the BOM header at the beginning of php; finally use the fwrite command to write the php file with the BOM header removed Just import it into a new PHP file.

How to remove php bom

PHP BOM removal example

PHP can use the ltrim() function to remove BOM. The function of this function is to move Except for blank characters or other predefined characters on the left side of the string, function syntax and parameter descriptions.

How to remove php bom

Here we take 1.php with BOM header as an example. 1.php is a simple hello world program. Next, we read the content of 1.php through 2.php. After removing the BOM header of 1.php, we write the modified content to 3.php, so that 3.php is a file without a BOM header.

How to remove php bom

The source code content of 2.php is as follows:

//php源码标识
<?php
//用fopen函数打开1.php文件
$file=fopen("1.php","r") or die ("unable to open file");
//读取1.php文件内容
$con=fread($file,filesize("1.php"));
//重点,调用ltrim将1.php开头的BOM头去掉。
$con=ltrim($con,"\XEF\XBB\XBF");
//创建3.php文件
$newfile=fopen("3.php","w") or die ("unable to create file");
//通过fwrite命令将去掉BOM头的1.php文件的内容写入到3.php
fwrite($newfile,$con);
//关闭文件
fclose($newfile);
fclose($file);
?>
Copy after login

How to remove php bom

Then execute 2.php through the php command, and the execution is completed Then a new 3.php file is generated.

How to remove php bom

Use the file and hexdump commands again to check that the BOM header of 1.php has been removed.

How to remove php bom

For more related knowledge, please visit PHP Chinese website!

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:
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