php代码掉bom的方法:1、使用“function clearbom($contents){...}”方式去掉文本中的bom头;2、通过“function checkbom ($filename) {...}”方法检测并去掉bom头;3、通过“function searchbom($string) {...}”方法搜索当前文件是否有bom并去除即可。
本教程操作环境:windows7系统、php8.1版、dell g3电脑。
php 代码怎么去掉bom?
php批量去掉utf8格式文件中的bom头部
我经常使用txt文本编辑器写php文件,所以经常会自动添加bom头部,导致在很多时候会带来问题,比如我们session无法工作、cookie无法设置等等问题。
下面我整理了几个利用php程序清除 utf8格式文件中的bom头部方法。
例1
代码如下
/*** 去掉文件中的 bom头* @var 0.1* @author chenwp*/function clearbom($contents){//utf8 去掉文本中的 bom头$bom = chr(239).chr(187).chr(191);return str_replace($bom,”,$contents);}/*** 去掉文件中的bom头* @param object $filename description* @return object description*/function clearfilebom($filename){$c = file_get_contents($filename);$c = clearbom($c);file_put_contents($filename,$c);}
例2
如何将带有bom文件的格式转换成无签名的utf-8格式文件呢?下面分享给大家一段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);}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);}//结束?>
例3
会自动扫描所有子目录和文件
代码如下
<?php// 设定你要清除bom的根目录(会自动扫描所有子目录和文件)$home = dirname(__file__);// 如果是windows系统,修改为:$win = 1;$win = 0;?><!doctype html public “-//w3c//dtd xhtml 1.0 transitional//en” “http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd”><html xmlns=”http://www.w3.org/1999/xhtml”><head><meta http-equiv=”content-type” content=”text/html; charset=utf-8″ /><title>utf8 bom 清除器</title><style>body { font-size: 10px; font-family: arial, helvetica, sans-serif; background: #fff; color: #000; }.found { color: #f30; font-size: 14px; font-weight: bold; }</style></head><body><?php$bombed = array();recursivefolder($home);echo ‘<h2>these files had utf8 bom, but i cleaned them:</h2><p>’;foreach ($bombed as $utf) { echo $utf .”<br />n”; }echo ‘</p>’;// 递归扫描function recursivefolder($shome) {global $bombed, $win;$win32 = ($win == 1) ? “\” : “/”;$folder = dir($shome);$foundfolders = array();while ($file = $folder->read()) {if($file != “.” and $file != “..”) {if(filetype($shome . $win32 . $file) == “dir”){$foundfolders[count($foundfolders)] = $shome . $win32 . $file;} else {$content = file_get_contents($shome . $win32 . $file);$bom = searchbom($content);if ($bom) {$bombed[count($bombed)] = $shome . $win32 . $file;// 移出bom信息$content = substr($content,3);// 写回到原始文件file_put_contents($shome . $win32 . $file, $content);}}}}$folder->close();if(count($foundfolders) > 0) {foreach ($foundfolders as $folder) {recursivefolder($folder, $win32);}}}// 搜索当前文件是否有bomfunction searchbom($string) {if(substr($string,0,3) == pack(“ccc”,0xef,0xbb,0xbf)) return true;return false;}?></body></html>
推荐学习:《php视频教程》
以上就是php 代码怎么去掉bom的详细内容。