批量 gb->utf-8编码转换. (转)

本文介绍了一个用于递归地遍历指定目录下所有文件,并将指定类型的文件从GB2312编码转换为UTF-8编码的PHP脚本。该脚本会修改包括.php、.html和.htm在内的文件,确保其内容和声明的字符集一致。

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

 

<?
//文件编码转换.
function file_iconv($fromcode,$tocode,$file){
    
$data = join('',file($file));
    
if(preg_match('/charset='.str_replace('-','/-',$tocode).'/mis',$data)){
        
echo "<font color='red'>$file  $tocode .</font>";
    }
    
$data = preg_replace('/charset='.str_replace('-','/-',$fromcode).'/mis',"charset=$tocode",$data);
    
$data = preg_replace('/(sets+namess+)''.str_replace('-','',$fromcode).''/mis',"/1'".str_replace('-','',$tocode)."'",$data);
    
$data = iconv("$fromcode","$tocode",$data);
    
$fp = fopen("$file","w");
    
fwrite($fp,$data);
    
fclose($fp);
    
echo "$file ...<br>";
}

function listfiles($dir="."){
    
    
$handle=opendir("$dir");
    
while ($file = readdir($handle)) {
        
if($file=="."||$file=="..")continue;
        
if(is_dir("$dir/$file")){
            listfiles(
"$dir/$file");
        }
        
else {
            
$ext = strtolower(strrchr($file,'.'));
            
if(in_array($ext,array(".php",".html",".htm"))){
                file_iconv(
"gb2312","utf-8","$dir/$file");
            }
        }
    }
    
closedir($handle);
}

listfiles(
"mydir");
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值