The reason and removal method of BOM header of UTF-8 File

Source: Internet
Author: User
1. What is the BOM header?In a UTF-8 encoded file, the BOM occupies three bytes in the file header to identify the file as a UTF-8 encoded file. 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 the cause of an error after editing the UTF-8 encoded PHP file in notepad.

2. Generate a header file containing BOMIn windows, use NotePad to open any text file and save it as UTF-8, so that the file will be automatically added with the BOM header information. Comparison:UTF-8 (including BOM header)UTF-8 (excluding BOM headers)From the comparison, we can see that the file containing the BOM header has three more bytes efbbbf.3. How to remove BOM header informationOpen the file with Notepad ++, select format-> In UTF-8 without BOM format encoding, and then save on the line. For example:4. automatically process BOM header information in a PHP Project (you only need to put this file under the project root directory and access it from the browser)

<? Php

If(Isset($ _ GET ['dir']) {// sets the file directory
$ Basedir = $ _ GET ['dir'];
}Else{
$ Basedir = '.';
}

Checkdir ($ basedir );

/**
* Traverse directories
* @ Param string $ basedir base Directory
*/
FunctionCheckdir ($ 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"). "<br> ";
}Else{
$ Dirname = $ basedir. "/". $ file;
Checkdir ($ dirname );
}
}
}
Closedir ($ dh );
}
}

/**
* Check the BOM header.
* @ Param string $ filename file name
* @ Param int $ whether auto is automatically processed. It is automatically processed by default.
*/
FunctionCheckBOM ($ filename, $ auto = 1 ){
$ 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 & amp; ord ($ charset [3]) = 191 ){
If($ Auto = 1 ){
$ Rest = substr ($ contents, 3 );
Rewrite ($ filename, $ rest );
Return("<Font color = red> BOM found, automatically removed. <a href = http://www.cnblogs.com/lfire/archive/2012/11/20/2778939.html> lfire blog </a> </font> ");
}Else{
Return("<Font color = red> BOM found. </font> ");
}
}
Else
Return("BOM Not Found .");
}

/**
* Rewrite a file
* @ Param string $ file to be rewritten
* @ Param mixed $ data the data to be rewritten
*/
FunctionRewrite ($ filename, $ data ){
$ Filenum = fopen ($ filename, "w ");
Flock ($ filenum, LOCK_EX );
Fwrite ($ filenum, $ data );
Fclose ($ filenum );
}

?>

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.