How to automatically add BOM header and explain BOM header and remove BOM header when downloading files in PHP

Source: Internet
Author: User
Tags php download
PHP download files automatically add BOM header, first of all to find out, what is the BOM header? When you save a text file in UTF-8 format with a program such as Notepad under Windows, Notepad adds several invisible characters (the EF BB BF) to the file header, which is the so-called BOM (Byte order Mark).

In the Utf-8 encoded file, the BOM in the file header, occupies three bytes, used to indicate that the file belongs to the Utf-8 encoding, now has a lot of software to identify the BOM header, but also some do not recognize the BOM header, such as PHP does not recognize the BOM header, This is also the reason why the error occurs when you edit Utf-8 encoding with Notepad.

Not limited to Notepad saved files, as long as the opening of the file contains several invisible characters of the EF BB BF (16 binary should be XEFXBBXBF, with binary edit file visible). This is like a conventional thing, when the system sees this thing, it will feel you this file is UTF-8 encoded.

If your interface is UTF-8, you need to force download a file, such as Csv.excel in the default (Chinese background), that the CSV is GB encoding, so if the meter has a BOM header, then you give the user to present files, may be garbled.

How to add BOM head?

Add the BOM header to the output file before you can:

The code is as follows:

The following describes the explanation of the BOM head and the method to remove

The way to get rid of BOM head, simple is the following two kinds:

1, EditPlus to the BOM head method

After the editor is resized to UTF8 encoded format, the saved file will be preceded by a string of hidden characters (also known as BOMs), used by the editor to identify whether the file is encoded in UTF8.

Run EditPlus, click Tools, select Preferences, select files, UTF-8 identity Select always delete signatures, and then edit and save php files without BOM.

2, UltraEdit removal of BOM head method

After opening the file, Save As option in the encoding format selected (Utf-8 without BOM header), OK is ok

It's easy to get rid of the BOM head.

Let's talk about UTF8 's BOM information.

BOM refers to the PHP file itself is stored in a BOM with the UTF-8, the normal page of Chinese garbled way is generally not caused by this reason.

Header ("content-type:text/html; Charset=utf-8 ");

This sentence controls the HTML output page encoding, the BOM only in Windows under the "notepad" stored as UTF-8, this can be used to winhex the beginning of the 2 bytes deleted.

In Dreamweaver inside the encoding settings can be set whether with a BOM, generally as long as PHP output is not a picture (GDI Stream), BOM will not cause problems.

GDI Stream appears as a red fork if there are additional characters at the beginning.

The above content to introduce the PHP download file automatically add BOM header and explain the BOM head and remove method, I hope you like.

The above describes how to automatically add the BOM header and the BOM header and the way to remove the BOM header, including the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.