递归删除utf8文件的bom头(该bom头可能导致php产生意外输出)

本文介绍了一个用PHP编写的脚本,该脚本能够批量处理指定目录下的文件,包括去除文件的BOM标记并重新写入文件。适用于需要批量处理文本文件的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不废话了, 是php代码, 如果要处理大文件, 可以改改, 这里是直接读入到数组中

//author: selfimpr //blog: http://blog.youkuaiyun.com/lgg201 //mail: lgg860911@yahoo.com.cn //EF BB BF这三个字节称为bom头 function hasbom(&$content) { $firstline = $content[0]; return ord(substr($firstline, 0, 1)) === 0xEF and ord(substr($firstline, 1, 1)) === 0xBB and ord(substr($firstline, 2, 1)) === 0xBF; } function unsetbom(&$content) { hasbom($content) and ($content[0] = substr($content[0], 3)); } function write($filename, &$content) { $file = fopen($filename, 'w'); fwrite($file, implode($content, '')); fclose($file); } function filenames($path) { $directory = opendir($path); while (false != ($filename = readdir($directory))) strpos($filename, '.') !== 0 and $filenames[] = $filename; closedir($directory); return $filenames; } function process($path) { $parent = opendir($path); while (false != ($filename = readdir($parent))) { echo $filename."/n"; if(strpos($filename, '.') === 0) continue; if(is_dir($path.'/'.$filename)) { process($path.'/'.$filename); } else { $content = file($path.'/'.$filename); unsetbom($content); write($path.'/'.$filename, $content); } } closedir($parent); } process('/home/selfimpr/t');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值