php输出多了几个隐形字符:utf-8文件头的问题
来源:百度文库 编辑:神马文学网 时间:2024/04/30 20:20:38
今天配置网络记事本接口站点的时候,通过软件获取接口返回信息前面总是多几个看不见的字符,经过多方检查,发现是utf-8文件头的问题。
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")."
";
}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);
}
?>
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")."
";
}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);
}
?>
php输出多了几个隐形字符:utf-8文件头的问题
[PHP Excel 输出]使用 PHP 输出带格式的 Excel 文件(转来的)
PHP的GBK与UTF8字符编码问题
用php实现gb2312和unicode(UTF-8)间的编码转换 - 无非 - PHPC...
字符编码笔记:ASCII,Unicode和UTF-8
字符编码详解及由来(UNICODE,UTF-8,GBK)[转帖]
字符编码笔记:ASCII,Unicode和UTF-8
字符编码笔记:ASCII,Unicode和UTF-8
字符编码详解及由来(UNICODE,UTF-8,GBK)
Utf-8和Gb2312乱码问题的终结
PHP中动态HTML的输出技术
头文件问题:iostream与iostream.h的区别
头文件中定义const 全局变量应注意的问题
预编译头文件的问题 - Thinking dynamic - 博客园
Unicode/UTF-8的区别
jspsmart字符编码的问题
php的几个配置文件函数
Wordpress中要注意的UTF-8的BOM问题 - 爱悠扬的日志 - 网易博客
JAVA字符编码系列一:Unicode,GBK,GB2312,UTF-8概念基础-在线阅读...
C++标准库提供了基于输入/输出流机制的文件操作
ASP Utf-8和Gb2312乱码问题的终结--痞子的窝
用UTF-8完全解决JSP MYSQL多国语言文字编码问题
C++头文件的作用
用 PHP 读取文件的正确方法