Php code sharing with bom header removed

Source: Internet
Author: User
Php code sharing with bom header removed
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.

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.