PHP and Unicode signature (BOM) _ PHP Tutorial

Source: Internet
Author: User
Tags blank page ultraedit
PHP and Unicode signature (BOM ). When writing or modifying php files stored in UTF-8 encoding, sometimes there are some inexplicable problems: 1. the page displays a blank text. 2. sometimes there are some inexplicable problems when you can't log on or you can't write or modify the php file that is saved in UTF-8 encoding:
1. the page displays the word "blank", and the rest is blank;
2. you cannot log in or log out;
3. a blank page appears at the top of the page;
4. an error warning is displayed on the top of the page;
5. other abnormal situations;
6. the generated image cannot be recognized by the browser.
Cause analysis:
When the file is saved in UTF-8 encoding, there are two situations: with Unicode signature (BOM) and without Unicode signature. BOM information is a hidden string starting with a file that is used by some editors to identify it as a UTF-8-encoded file.

Similar to WINDOWS Notepad and other software, when saving a file encoded in UTF-8, will insert three invisible characters at the beginning of the file (0xEF 0xBB 0xBF, BOM -- Byte Order Mark ). It is a string of hidden characters, used for the notepad editor to identify whether the file is encoded in UTF-8. For general files, this will not cause any trouble.

But for PHP, PHP did not consider the BOM issue during design, does not ignore the three characters at the beginning of the UTF-8-encoded file BOM, the BOM is used as a part of the beginning body of the file. Because it must be in
Solution:

When editing or changing any text files, you must use an editor without adding BOM. The editor in Linux should have no such problem. In WINDOWS, do not use notepad or other editors. Recommended editors are: Editplus version 2.12 and later; EmEditor; UltraEdit (options related to 'add BOM 'need to be removed); Dreamweaver (options related to 'add BOM' need to be removed.

If you want to cancel a file that has been added with BOM, you can use the editor above to save it again. (Editplus needs to be saved as gb first, then as UTF-8 .)

Ultraedit, editplus, notepad four tools for UTF-8 support is different, the following is a summary of the four tools for UTF-8 support:
UTF-8 BOM header: three characters: ef bb bf.
1. notepad
When saving notepad, select the UTF-8 format, will write BOM header in the file header.
2. editplus
When saving the file, select the UTF-8 format without writing BOM header in the file header.
3. ultraedit
Ultraedit has the most comprehensive support for UTF-8. In advanced-> configuration, you can select whether to write BOM header when saving the file.
4. vi
Refers to vim in Linux, if the UTF-8 file has a BOM header at the beginning, it can normally display the UTF-8 encoding, otherwise, the display is garbled.
There are also some encoding and conversion tools. for example, you can use java to write a simple encoding and conversion tool. These tools will not add BOM headers.

Appendix:

The configuration of UltreEdit closes the option 'write UTF-8 header markup (BOM) for all UTF-8 while saving. That is: Write UTF-8 BOM header to ALL UTF-8 files when saved OFF.
Different versions may not exist. Instead, you need to disable "automatic utf8 detection. The utf8 file is garbled.
Editplus is recommended.


Check whether the file in the directory has a BOM program:
// This file is used to quickly test whether the UTF-8 encoded file is added with BOM and can be automatically removed.
// By Bob Shen

$ Basedir = "."; // modify the directory to be checked for this behavior. the vertex indicates the current directory.
$ Auto = 0; // whether to automatically remove the detected BOM. 1 is yes, 0 is no.

// Do not change the following

If ($ dh = opendir ($ basedir )){
While ($ file = readdir ($ dh ))! = False ){
If ($ file! = '.' & $ File! = '..'&&! Is_dir ($ basedir. "/". $ file) echo "filename: $ file". checkBOM ("$ basedir/$ file ")."
";
}
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 );
}
?>

Login 1. the page displays a blank text. 2. you cannot log on or log on...

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.