PHP batch removes BOM header content code

Source: Internet
Author: User
This article mainly introduces related information about the code for removing BOM header content in PHP in batches. For more information, see What is the bom header?

In a UTF-8 encoded file, BOM occupies three bytes in the file header to indicate that the file belongs to UTF-8 encoding. Currently, many software programs have recognized the bom header, but some cannot recognize the bom header, for example, PHP cannot recognize the bom header, which is also the cause of an error after UTF-8 encoding is edited in Notepad.

The code for batch bom header removal is as follows:

<? Php if (isset ($ _ GET ['dir']) {// sets the file directory $ 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 ")."
";} 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 ("BOM found, automatically removed. _ http://www.joyphper.net ");} else {return (" BOM found. ") ;}} else return (" BOM Not Found. ");} function rewrite ($ filename, $ data) {$ filenum = fopen ($ filename," w "); flock ($ filenum, LOCK_EX); fwrite ($ filenum, $ data); fclose ($ filenum) ;}?>

PS: To remove the bom header, there are two simple methods:

1. how to remove the BOM header from editplus

After the editor is adjusted to the UTF8 encoding format, a hidden character (BOM) is added before the saved file, which is used by the editor to identify whether the file is UTF-8 encoded.

Run Editplus, Click Tools, select Preferences, select files, UTF-8 ID select always delete signature,

Then, the edited and saved php file does not contain BOM.

2. how to remove the bom header from ultraedit

After opening the file, select the encoding format of the save as option (UTF-8 without bom header). OK.

How about removing the bom header?

Let's talk about the BOM information of utf8.

BOM refers to the PHP file storage method for the UTF-8 with BOM, the common page of Chinese garbled mode is generally not caused by this reason.

header("Content-type: text/html; charset=utf-8");

This statement controls the encoding of html output pages,

BOM is only available in WINDOWS when the "notepad" storage for the UTF-8, this can be used to remove the first two bytes with WINHEX.

You can set whether to include BOM in the code settings in dreamweaver. generally, BOM will not cause problems as long as the output of php is not an image (GDI Stream.
If there are additional characters at the beginning of the GDI Stream, it will be displayed as a Red Cross.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.