Home  >  Article  >  Backend Development  >  How to batch modify php files

How to batch modify php files

coldplay.xixi
coldplay.xixiOriginal
2020-09-07 10:22:303455browse

批量修改php文件的方法:首先打开目录;然后列出所有文件并去掉【.】和【..】,代码为【if($file!='.' &&$file!='..')】;接着赋值给数组,代码为【$arr_file[]=$file】,最后输出结果。

How to batch modify php files

批量修改php文件的方法:

【相关学习推荐:php编程(视频)】

<?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))
{
//列出所有文件并去掉&#39;.&#39;和&#39;..&#39;
    if($file!=&#39;.&#39; &&$file!=&#39;..&#39;)
    {
        //$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.&#39;\\&#39;.$name."_en.php","w");
        $fr = fopen($folder.&#39;\\&#39;.$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);
?>

想了解更多编程学习,敬请关注php培训栏目!

The above is the detailed content of How to batch modify php files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn