Php stealth character 65279 (BOM header of UTF-8)

Source: Internet
Author: User
Tags ultraedit
This article mainly introduces the solution to the problem of invisible character 65279 in php, which is actually the BOM header of the UTF-8 file. This article provides a small program to delete the BOM header of the php file, for more information, see CSDN. I found a problem with php output blank and invisible character 65279 on the internet. I sent this 65279 character to php to mark the file as UTF-8 encoded, the output will be output to the client together, causing the client to fail to match the string if ajax is used to obtain the return value.

Php stealth character 65279 is explained as follows:

UTF-8 files can be divided into two formats: BOM and BOM.

What is BOM?

"Ef bb bf" these three bytes are called BOM, full name is "Byte Order Mard ". In utf8 files, BOM is often used to indicate that this file is a UTF-8 file, and BOM is intended to be used in utf16.

The bom is output when the UTF-8 file is output in php. therefore, to use UTF-8 in php, you must use a UTF-8 file without the bom header.

The commonly used text editing software supports UTF-8 file storage in different ways. pay special attention when using it.

For example:

1. when you use ultraedit, there are two options for "UTF-8" and "UTF-8-no BOM" when you save it.
2. Windows Notepad stores bom.
3. different versions of the EditPlus software support different UTF-8 storage features. for example, Version 2.31 saves data without bom and version 2.11 saves data with bom.

Remove the UTF-8 file header:

1. use ultraedit to save, select "UTF-8-no BOM"
2. a very useful php program running in the root directory of the site will remove the bom headers of all UTF-8 files in the directory. 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) {        if ($file != '.' && $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) { $rest = substr($contents, 3); rewrite ($filename, $rest); return ("BOM found, automatically removed."); } 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); }

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.