PHP batch removes BOM header information code, and php removes bom code
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 "). "<br>";} 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 ($ chars Et [1]) = 239 & ord ($ charset [2]) = 187 & 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.joyphper.net> http://www.joyphper.net </a> </font> ");} else {return (" <font color = red> BOM found. </font> ") ;}} else return (" BOM Not Found. ");} function rewrite ($ filename, $ d Ata) {$ 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.
Articles you may be interested in:
- Batch remove the PHP code of bom in the PHP File
- How to Use PHP to batch remove UTF8 BOM information of Files
- PHP code example for batch Delete, clear UTF-8 file BOM Header
- PHP batch detection and removal of file BOM header code instances
- PHP batch remove BOM header code sharing