PHP 批量更改文件名和文件内容
刚开始打开\xampp下的test文件死活不成功,后来发现\t和\x都是转义字符,要多加一个反斜杠
写小程序思路清晰最重要,效率反而还是其次的
程序改自网络,所以保留一些注释
<?php
$folder="D:\U";
//打开目录
$fp=opendir($folder);
$find=array("admanage","admin","adtype","maintype","diymenu","getmode","goods","goodsattr","goodsbrand","goodsflag","goodsorder","goodsreview","goodstype","infoflag","infoclass","infoimg","infolist","job","lnk","member","message","paymode","postarea","postmode","soft","vote","webconfig","weblink","weblinktype","info");
$replace=array("admanage_en","admin_en","adtype_en","maintype_en","diymenu_en","getmode_en","goods_en","goodsattr_en","goodsbrand_en","goodsflag_en","goodsorder_en","goodsreview_en","goodstype_en","infoflag_en","infoclass_en","infoimg_en","infolist_en","job_en","lnk_en","member_en","message_en","paymode_en","postarea_en","postmode_en","soft_en","vote_en","webconfig_en","weblink_en","weblinktype_en","info_en");
$cnt=count($find);
//阅读目录
while(false!=$file=readdir($fp))
{
//列出所有文件并去掉'.'和'..'
if($file!='.' &&$file!='..')
{
//$file="$folder/$file";
$file="$file";
//赋值给数组
$arr_file[]=$file;
}
}
//输出结果
if(is_array($arr_file))
{
while(list($key,$value)=each($arr_file))
{
$name=strrev(substr(strrev($value),4));
$fw = fopen($folder.'\\'.$name."_en.php","w");
$fr = fopen($folder.'\\'.$value, "r");
while (!feof($fr))
{
$line = fgets($fr);
if(strstr($line,"#@__"))
{
for($i=0;$i<$cnt;$i++)
if(strstr($line,$find[$i]))
{
$line=str_replace($find[$i],$replace[$i],$line);
break;
}
}
fputs($fw,$line);
}
fclose($fr);
fclose($fw);
}
}
//关闭目录
closedir($fp);
?>