批量去除PHP文件中bom的PHP代码
2023-06-13 09:14:33 时间
需要去除BOM,就把附件里的tool.php文件放到目标目录,然后在浏览器访问tool.php即可!
复制代码代码如下:
<?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);
}
functioncheckBOM($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("<fontcolor=red>BOMfound,automaticallyremoved.</font>");
}else{
return("<fontcolor=red>BOMfound.</font>");
}
}else
return("BOMNotFound.");
}
functionrewrite($filename,$data){
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?>
PHP批量去除PHP文件中bom的代码
<?php
if(isset($_GET["dir"])){//设置文件目录
$basedir=$_GET["dir"];
}else{
$basedir=".";
}
$auto=1;
checkdir($basedir);
functioncheckdir($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);
}
}
functioncheckBOM($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("<fontcolor=red>BOMfound,automaticallyremoved._<ahref=http://www.joyphper.net>http://www.joyphper.net</a></font>");
}else{
return("<fontcolor=red>BOMfound.</font>");
}
}
elsereturn("BOMNotFound.");
}
functionrewrite($filename,$data){
$filenum=fopen($filename,"w");
flock($filenum,LOCK_EX);
fwrite($filenum,$data);
fclose($filenum);
}
?>
相关文章
- 【说站】php中require和include如何区分
- php案例 批量删除某个目录下的文件
- PHP-解决导出excel文件中数字过大科学计数法问题
- CentOS 8.1下搭建LEMP(Linux+Nginx+MySQL+PHP)环境(教程详解)
- 卸载Linux环境下的PHP:一个指南(linux如何卸载php)
- PHP创建文件命名中文乱码解决的方法详解编程语言
- PHP的CURL详解编程语言
- 借助 PHP 与 MySQL 轻松实现文件下载(php和mysql下载)
- Linux下打开PHP文件的实用技巧(linux打开php文件)
- 环境PHP和MySQL环境搭建:快速、安全、便捷(php和mysql配置)
- PHP中Redis的应用场景(redis应用场景PHP)
- PHP轻松加载MySQL模块(php加载mysql模块)
- 如何在Linux系统下执行PHP文件?(linux下执行php)
- PHP实现在Linux系统中进行文件操作(php写linux文件)
- PHP文件快速导入MySQL数据库(php文件导入mysql)
- PHP如何开启MSSQL支持?(php怎么开启mssql)
- PHPMSSQL连接数据库配置端口最佳实践(php mssql 端口)
- PHP增加了对.ZIP文件的读取功能
- PHP读写文件的方法(生成HTML)
- PHP学习文件处理与文件上传课件
- php文件上传后缀名与文件类型对照表(几乎涵盖所有文件)
- 《PHP编程最快明白》第五讲:php目录、文件操作
- PHP和Mysqlweb应用开发核心技术第1部分Php基础-1开始了解php
- php入门学习知识点六PHP文件的读写操作代码
- PHP数组操作汇总php数组的使用技巧
- 用来解析.htgroup文件的PHP类
- php打开文件fopen函数的使用说明
- PHP改进计算字符串相似度的函数similar_text()、levenshtein()
- 叫你如何修改Nginx与PHP的文件上传大小限制