PHP Bulk Removal BOM Header content information Code _php instance

Source: Internet
Author: User
Tags ord

What is a BOM header?

In the Utf-8 encoded file BOM in the file head, occupies three bytes, used to indicate that the file belongs to the Utf-8 code, now has a lot of software to identify the BOM header, but some do not recognize the BOM header, such as PHP can not identify the BOM header, This is also the reason why you can make mistakes when you edit the Utf-8 encoding with Notepad.

The bulk removal of the BOM header code looks like this:

<?php if (isset ($_get[' dir ')) {//Set 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 ($charset [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&GT;HTTP://WWW.JOYPHPER.net</a></font> "); 
else {return ("<font Color=red>bom found.</font>"); 
' Else return (' BOM not Found. '); 
function rewrite ($filename, $data) {$filenum = fopen ($filename, "w"); 
Flock ($filenum, LOCK_EX); 
Fwrite ($filenum, $data); 
Fclose ($filenum);  }?>

PS: Remove the method of BOM head, simple is the following two kinds:

1, EditPlus to the method of BOM head

After the editor adjusts to the UTF8 encoding format, the saved file is preceded by a string of hidden characters (also the BOM), which is used by the editor to identify whether the file is encoded in UTF8.

Run EditPlus, click Tools, select Preferences, select files, UTF-8 identity option always delete signature,

Then the php file edited and saved after the PHP file is not with the BOM.

2, UltraEdit remove the BOM head method

After opening the file, save as an option in the encoding format selected (Utf-8 no BOM header), ok OK

It's easy to get rid of the BOM header.

Another piece of discussion utf8 BOM information

BOM refers to the storage of the PHP file itself as a UTF-8 with the BOM, ordinary pages of Chinese garbled method is generally not caused by this reason.

Header ("content-type:text/html; Charset=utf-8 ");

This sentence controls how the HTML output page is encoded,

The BOM is only available when the "notepad" is stored as UTF-8 under Windows, which can be deleted by Winhex the 2 bytes that begin with.

Within the Dreamweaver coding settings can be set up with a BOM, generally as long as the output of PHP is not a picture (GDI Stream), the BOM will not cause problems.
GDI Stream appears as a red fork if it has an extra character at the beginning.

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.