How to remove BOM header for UTF-8 coding?

Source: Internet
Author: User
If you use uft8 encoding or page creation, you will find that the page is blank when you save the page, but the page does have content. then you will hear that it is a bom header problem, so what is the bom header? how can we solve the page blank problem caused by the bom header? let's take a look at it together... if you use uft8 encoding or page creation, you will find that the page is blank when you save the page, but the page does have content. then you will hear that it is a bom header problem, so what is the bom header? how can we solve the page blank problem caused by the bom header? let's take a look at the specific solution.

The Unicode specification has the concept of BOM. BOM -- Byte Order Mark, which is a Mark of the Byte Order. Here is a description of BOM:

There is a character named "zero width no-break space" in the UCS encoding, and its encoding is FEFF. FFFE does not exist in the UCS, so it should not appear in actual transmission. We recommend that you transmit the character "zero width no-break space" before transmitting the byte stream in the UCS specification ". In this way, if the receiver receives FEFF, it indicates that the byte stream is Big-Endian; if it receives FFFE, it indicates that the byte stream is Little-Endian. Therefore, the character "zero width no-break space" is also called BOM.

The UTF-8 does not need BOM to indicate the byte order, but BOM can be used to indicate the encoding method. The UTF-8 code for the character "zero width no-break space" is ef bb bf. So if the receiver receives a byte stream starting with ef bb bf, it will know that this is UTF-8 encoding.

Windows uses BOM to mark the encoding of text files.

In addition, the unicode website FAQ-BOM detailed BOM. The natural authority of the official website is only in English, and it looks hard.

In the UTF-8 encoded file, BOM occupies three bytes. if you use notepad to save a text file as a UTF-8 encoding method, open the file with UE, switch to the hexadecimal editing status to see the beginning of FFFE, this is a good way to identify the UTF-8 encoding file, the software through BOM to identify whether the file is UTF-8 encoding, many software also require BOM to be included in the file to be read.

However, there are still a lot of software that cannot recognize BOM. I learned when studying Firefox that in earlier versions of Firefox, extensions cannot have BOM, however, Firefox 1.5 and later versions have started to support BOM, and now it is found that PHP does not support BOM.

PHP didn't consider BOM issues during design, that is, he would not ignore the three characters at the beginning of the BOM in the UTF-8-encoded file, because

Remove BOM header?

The simplest way to remove the BOM header is to use editplus, ultraedit, and other software. The details are as follows:

1. use editplus to remove the BOM header

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

Run Editplus, click the tool, select Preferences, select the file, select the UTF-8 ID always delete the signature, and then the php file after editing and saving the php file is without BOM.

2. use ultraedit to remove the BOM header

Open the file, "save as" option in the encoding format selection, UTF-8 without BOM header, OK.

3. use php to batch remove all file BOM headers. the code is as follows:

 ";                } 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.");        } 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);}


Address:

Reprinted at will, but please attach the article address :-)

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.