帝国CMS模板大全
www.admin99.cn
www.92cms.cn 帝国CMS模板下载站!,情怀,养站,二次开发!源码需求比较大的一站式会员下载,价更省!!!

php一个批量转换文件为utf-8的脚本

最近看到 一个 很古老的代码,当时正是gb2312横行的时代,所以,遍地都是gbk编码了。现如今打开,看起来一堆乱码,很不舒服,写了 一个 凑合脚本。

结合shell 跟 php

shell 实际上用的只是find

if [ $# -lt 1 ]
then
depth=1
else
depth=$1
fi

if [ $# -lt 2 ]
then
name="*"
else
name="$2"
fi

echo "Begin running:"

find . -maxdepth $depth -name "$name" -exec php /Users/qixingyue/sina/bin/cutf_8
.php {} \;

核心php代码:

<?php

if($argc != 2) exit();
$file_name = $argv[1];

if(is_dir($file_name)) return;

$file_name = getcwd() . trim($file_name,".");

if(is_link($file_name)){
echo "Link file: $file_name \n";
return;
}

if(!file_exists($file_name)){
echo "None file: $file_name \n";
return;
}

if(!is_writeable($file_name)){
echo "Can not write $file_name \n";
return;
}

$str = file_get_contents($file_name);

$from_types = array("ASCII", "GBK", "GB2312", "Unicode", "UTF-8");
$this_type = mb_detect_encoding($str, $from_types);

if($this_type == "UTF-8") {
echo "Already UTF-8 $file_name \n";
return;
}

if($this_type){

echo "$file_name $this_type ========> UTF-8 \n";
$str = mb_convert_encoding($str,"UTF-8",$this_type);
file_put_contents($file_name,$str);
}

赞(0)
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权
文章名称:《php一个批量转换文件为utf-8的脚本》
文章链接:https://www.admin99.cn/8026
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
QQ站长交流群:953950264

登录

找回密码

注册