首页 >

用PHP解决万恶的BOM

后端开发|php教程用PHP解决万恶的BOM
用,PHP,解决,万恶,BOM,php,path,dirname,FILE,scandDir,function,bo
后端开发-php教程
[php]
<?php
$path = dirname(__FILE__);
如何下载linux内核源码,码流分析 Ubuntu,tomcat9怎么卸载,python 财经爬虫,php怎么导入mysql,宁河区一站式seo服务成本lzw
scandDir($path);
asp access注入源码,vscode 1.21,ubuntu 升级失败,web tomcat,sqlite 跨进程,ftp服务器集群,弹出层插件实现,前端js小框架,r爬虫与python爬虫,vb和php,神马网站seo排名,网站源码后门扫描,网页上点击按钮弹出选择框,siteweaver 6.8 模板,将div固定在页面底部,图文网站管理系统源码,买卖电话号什么程序lzw
function bomtoutf8($filename)
{
$ef = 239;
$bb = 187;
$bf = 191;
android post 源码,ubuntu退出安装时,Tomcat错误地址重定向,爬虫脑 觉醒,php取mysql记录,辽宁运营抖音seo优化平均价格lzw
if (is_dir($filename)) return;
$fd = fopen($filename, “rb”);
if (!$fd) exit($filename);
$headers = fread($fd, 3);
fclose($fd);

if (strlen($headers) < 3) return; if (ord($headers[0]) == $ef &&
ord($headers[1]) == $bb &&
ord($headers[2]) == $bf
) {
echo $filename, “\n”;
$content = file_get_contents($filename);
if (!$content) {
exit(“file get contents failed”);
}
$len = strlen($content);
$content = substr($content, 3);

if ($content === false && $len!=3) {
exit(“sbustr failed”);
} else if ($content === false && $len==3){
$content = “”;
}
$ret = file_put_contents($filename, $content, LOCK_EX);
if ($ret === false) {
exit(“file put contents failed”);
}
echo “successed file “, $filename, “\n”;
}

}

function scandDir($path)
{
$d = dir($path);
if (!$d) exit(“dir failed”);

while (false !== ($entry = $d->read())) {
if ($entry == ‘.’ || $entry == ‘..’) {
continue;
}
$filename = $path . DIRECTORY_SEPARATOR . $entry;
// echo $filename, “\n”;
if (is_dir($filename)) {
scandDir($filename);
} else {
bomtoutf8($filename);
}
}
}

<?php
$path = dirname(__FILE__);

scandDir($path);

function bomtoutf8($filename)
{
$ef = 239;
$bb = 187;
$bf = 191;

if (is_dir($filename)) return;
$fd = fopen($filename, “rb”);
if (!$fd) exit($filename);
$headers = fread($fd, 3);
fclose($fd);

if (strlen($headers) < 3) return;

if (ord($headers[0]) == $ef &&
ord($headers[1]) == $bb &&
ord($headers[2]) == $bf
) {
echo $filename, “\n”;
$content = file_get_contents($filename);
if (!$content) {
exit(“file get contents failed”);
}
$len = strlen($content);
$content = substr($content, 3);

if ($content === false && $len!=3) {
exit(“sbustr failed”);
} else if ($content === false && $len==3){
$content = “”;
}
$ret = file_put_contents($filename, $content, LOCK_EX);
if ($ret === false) {
exit(“file put contents failed”);
}
echo “successed file “, $filename, “\n”;
}

}

function scandDir($path)
{
$d = dir($path);
if (!$d) exit(“dir failed”);

while (false !== ($entry = $d->read())) {
if ($entry == ‘.’ || $entry == ‘..’) {
continue;
}
$filename = $path . DIRECTORY_SEPARATOR . $entry;
// echo $filename, “\n”;
if (is_dir($filename)) {
scandDir($filename);
} else {
bomtoutf8($filename);
}
}
}

友情提示:代码是批量处理目录下(包括子目录)的所有带BOM文件,使用前需备份

http://www.liuzhongwei.com/PHPjc/477261.htmlwww.liuzhongwei.comtruehttp://www.liuzhongwei.com/PHPjc/477261.htmlTechArticle[php] ?php $path = dirname(__FILE__); scandDir($path); function bomtoutf8($filename) { $ef = 239; $bb = 187; $bf = 191; if (is_dir($filename)) return; $fd = fopen($filename, rb); i…

用PHP解决万恶的BOM
  • Mysql数据库操作类( 1127版,提供源码下载 )
  • Mysql数据库操作类( 1127版,提供源码下载 ) | Mysql数据库操作类( 1127版,提供源码下载 ) ...

    用PHP解决万恶的BOM
  • php日期所在月的天数
  • php日期所在月的天数 | php日期所在月的天数 ...

    用PHP解决万恶的BOM
  • PHP中父类与子类的晚期绑定/延迟绑定
  • PHP中父类与子类的晚期绑定/延迟绑定 | PHP中父类与子类的晚期绑定/延迟绑定 ...