很多站长朋友们都不太清楚php去掉bom,今天小编就来给大家整理php去掉bom,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 php 如何删除文件里面的bom 2、 什么是Bom头 怎样去除PHP文件的Bom头 3、 PHP批量删除、清除UTF-8文件BOM头的代码实例 4、 如何去掉utf-8的BOM的问题 php 如何删除文件里面的bom<?php
//此文件用于快速测试UTF8编码的文件是不是加了BOM,并可自动移除
$basedir="."; //修改此行为需要检测的目录,点表示当前目录
$auto=1; //是否自动移除发现的BOM信息。1为是,0为否。
//以下不用改动
if ($dh = opendir($basedir)) {
while (($file = readdir($dh)) !== false) {
if ($file!='.' $file!='..' !is_dir($basedir."/".$file)) echo "filename: $file ".checkBOM("$basedir/$file")." <br>";
}
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头 怎样去除PHP文件的Bom头方法一:用Ultraedit或Editplus打开PHP文件,另存为无Bom的utf-8文件方法二:用Dreamweaver去除Bom头
菜单–修改–页面属性 或者ctrl+j打开页面属性窗口,点选“标题/编码”去掉“包括Unicode签名Bom”前的勾
有Bom的文件太多?这样太麻烦?当然有更好的方法!方法三:用php文件批量去除bom头 将以下这段代码保存为php文件,上传到服务器,用浏览器访问它!<?phpif (isset($_GET['dir'])){ //设置文件目录
$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 (<font color=redBOM found, automatically removed.</font);} else {return (<font color=redBOM found.</font);}}else return (BOM Not Found.);}function rewrite ($filename, $data) {
$filenum = fopen($filename, w);
flock($filenum, LOCK_EX);
fclose($filenum);}?还用记事本保存?那你真是智商捉急了!
PHP批量删除、清除UTF-8文件BOM头的代码实例记得运行代码前先把文件备份一下哦,避免出现失败问题。
代码一:
复制代码
代码如下:
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.");
}
代码二:
复制代码
代码如下:
<?php
header('content-Type:
text/html;
charset=utf-8');
if(isset($_GET['dir'])){
//设置文件目录,如果没有设置,则自动设置为当前文件所在目录
$basedir=$_GET['dir'];
}else{
$basedir='.';
}
$auto=1;/*设置为1标示检测BOM并去除,设置为0标示只进行BOM检测,不去除*/
echo
'当前查找的目录为:'.$basedir.'当前的设置是:';
echo
$auto?'检测文件BOM同时去除检测到BOM文件的BOM<br
/>':'只检测文件BOM不执行去除BOM操作<br
/>';
checkdir($basedir);
function
checkdir($basedir){
if($dh=opendir($basedir)){
while
(($file=readdir($dh))
!==
false){
if($file
!=
'.'
$file
!=
'..'){
if(!is_dir($basedir.'/'.$file)){
echo
'文件:
'.$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并已自动去除</font>');
}else{
return
('
<font
color=red>找到BOM</font>');
}
}else{
return
('
没有找到BOM');
}
}
function
rewrite($filename,$data){
$filenum=fopen($filename,'w');
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?>
如何去掉utf-8的BOM的问题去掉utf-8的BOM的方法如下:
1、editplus去BOM头的方法
编辑器调整为UTF8编码格式后,保存的文件前面会多出一串隐藏的字符(也即是BOM),用于编辑器识别这个文件是否是以UTF8编码。 运行Editplus,点击工具,选择首选项,选中文件,UTF-8标识选择 总是删除签名,然后对PHP文件编辑和保存后的PHP文件就是不带BOM的了。
2、ultraedit去除bom头办法
打开文件后,另存为选项的编码格式里选择(utf-8 无bom头),确定即可。
3、放在项目根目录,然后运行。
关于php去掉bom的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php去掉bom php去掉指定字符的详细内容...