PHP hidden character 65279 (utf-8 BOM header) Problem _php tips

Source: Internet
Author: User
Tags ord ultraedit

Today in csdn see a PHP output blank stealth character 65279 problem, found on the Internet under, send this 65279 characters is PHP to mark the file is utf-8 encoded, the output will be output together to the client, resulting in the client if using AJAX to get the return value, Unable to match string.

The PHP stealth character 65279 is interpreted as follows:

UTF-8 encoded files can be divided into no BOM and BOM two kinds of formats.

What is a BOM?

"EF BB BF" This three bytes is called the BOM, the full name is "byte order Mard". In the UTF8 file commonly used BOM to indicate that this file is UTF-8 file, and the original BOM is in Utf16.

Utf-8 file output in PHP when the BOM will be output, so to use Utf-8 in PHP, you must use the Utf-8 file without the BOM header.

Commonly used text editing software for Utf-8 file preservation is not the same way, use the time to pay special attention to.

For example:

1, when the use of UltraEdit, there will be "UTF-8" and "UTF-8-no BOM" two options.
2, the window Notepad is saved with the BOM.
3, EditPlus software different versions of the Utf-8 save support, such as: 2.31 version of the Save is not with the BOM, 2.11 version of the saved with a BOM.

The way to remove the Utf-8 file head:

1, use UltraEdit to save, select "UTF-8-No BOM"
2, a very useful PHP program, placed in the site root directory to run, will be the directory of all utf-8 file BOM head removed, the code is as follows:

Remove the Utf-8 BOMs//by magicbug at gmail dot com if (isset ($_get[' dir ')) {//config the Basedir $basedir 
 =$_get[' dir ']; 
 }else{$basedir = '. '; 
  
 } $auto = 1; 
  
 Checkdir ($basedir); function Checkdir ($basedir) {if ($dh = Opendir ($basedir)) {while ($file = Readdir ($DH))!== false) {I F ($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.</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);  }

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.