This article introduces the example code for removing the bom header in php. the bom occupies three bytes in the file header and sometimes needs to be removed. The example provided in this article is for your reference. The php code for removing the bom header 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. _ csdn.net ");} 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) ;}?> Appendix. bom header information description. In a UTF-8 encoded file, BOM occupies three bytes in the file header to indicate that the file belongs to UTF-8 encoding. PHP cannot recognize the bom header. if you use notepad to edit UTF-8 encoding, an error occurs. Remove the bom header. 1. after the editplus method editor goes to the BOM header and is changed to the UTF8 encoding format, a hidden string (that is, BOM) appears before the saved file ), used by the editor to identify whether the file is UTF-8 encoded. Run Editplus, click the tool, select preference, 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. remove the bom header in ultraedit. after opening the file, save it as an option and select the encoding format (UTF-8 without bom header. 3. The program specially written to remove the file BOM header is published now. it can be placed in the project root directory and then run. |