PHP Bulk Removal BOM Header content information Code _php instance

Source: Internet
Author: User
what is a BOM header?

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

The bulk removal BOM header code is as follows:

<?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! = '. ' &A mp;& $file! = ' ... ') {if (!is_dir ($basedir. ") /". $file)" {echo "filename: $basedir/$file". Checkbom ("$basedir/$file"). "
"; } 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) {$re st = substr ($contents, 3); Rewrite ($filename, $rest); Return ("BOM found, automatically removed._http://www.joyphper.net"); } else {return ("BOM found.");}} else return ("BOM not Found."); The function rewrite ($filename, $data) {$filenum = fopen ($filename, "w"), Flock ($filenum, LOCK_EX); Fwrite ($filenum, $data ); Fclose ($filenum); }?>

PS: The way to get rid of BOM head, simple is the following two kinds:

1, EditPlus to the BOM head method

After the editor is resized to UTF8 encoded format, the saved file will be preceded by a string of hidden characters (also known as BOMs), used by the editor to identify whether the file is encoded in UTF8.

Run EditPlus, click Tools, select Preferences, select File, UTF-8 identity Select Always delete signature,

Then the PHP files are edited and saved by the PHP file is not with the BOM.

2, UltraEdit removal of BOM head method

After opening the file, Save As option in the encoding format selected (Utf-8 without BOM header), OK is ok

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

Let's talk about UTF8 's BOM information.

BOM refers to the PHP file itself is stored in a BOM with the UTF-8, the normal page of Chinese garbled way is generally not caused by this reason.

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

This sentence controls how HTML output pages are encoded,

The BOM is only available when the "notepad" store is stored as UTF-8 in Windows, which can be deleted with the Winhex of the first 2 bytes.

In Dreamweaver inside the encoding settings can be set whether with a BOM, generally as long as PHP output is not a picture (GDI Stream), BOM will not cause problems.
GDI Stream appears as a red fork if there are additional characters 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.