php中隐形字符65279(utf-8的BOM头)问题,65279bom
php中隐形字符65279(utf-8的BOM头)问题,65279bom
今天在CSDN看到一个php输出空白隐形字符65279的问题,在网上找了下,发下这个65279字符是php用来标记文件是utf-8编码的,输出的时候会一起输出到客户端,导致客户端如果使用ajax得到返回值时,无法匹配字符串。
php隐形字符65279解释如下:
UTF-8 编码的文件可以分为无 BOM 和 BOM 两种格式。
何谓BOM?
"EF BB BF" 这三个字节就叫BOM,全称是"Byte Order Mard"。在utf8文件中常用BOM来表明这个文件是UTF-8文件,而BOM的本意是在utf16中用。
utf-8文件在php中输出的时候bom是会被输出的,所以要在php中使用utf-8,必须要是使用不带bom头的utf-8文件。
常用的文本编辑软件对utf-8文件保存的支持方式并不一样,使用的时候要特别留意。
例如:
1、使用ultraedit时,另存时会有“UTF-8”和“UTF-8 - 无BOM”两种选择。
2、 window的记事本保存的是带bom的。
3、EditPlus软件不同版本对utf-8的保存支持不一样,例如:2.31版本保存的是不带bom的,2.11版本保存的是带bom的。
把utf-8文件头去掉的办法:
1、使用ultraedit另存,选择“UTF-8 - 无BOM”
2、一个很有用的php程序,放在站点根目录下运行,会把目录下全部utf-8文件的bom头去掉,代码如下:
//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")." <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); }
第一,BOM是不会影响你显示的绎码的……
如果你手动绎码后,能正常 。那是因为你没在你的文件指定字符串编码
header("Content-type:text/html;charset=utf8");在PHP文件开头加上这句
保存时,使用UTF-8 无BOM编码格式。
php在处理BOM头的时候,有时候存在错误,可能造成你在使用 header 或 session_start 之类的函数时,出现 文件已经输出的错误,多数都是因为BOM头送出去了。。因为在php看来,成了一个空格。所以使用无BOM的格式!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
