// 读取所有文件
$dirPath = __DIR__.'/replace/'; // 文件夹路径
$fileKeywords = '.htm'; // 文件的关键词,主要用于区分其他文件,如文件后缀等。
$pathArr = fileList($dirPath,$fileKeywords);
// 修改文件的内容并替换
foreach ($pathArr as $item){
$content = file_get_contents($item);
$content = str_replace('搜索的字符','替换的字符',$content);
file_put_contents($item,$content);
echo "处理完成--:".$item."\n";
}
/**
* 获取文件夹内的所有文件
* @param $dirPath // 文件夹路径
* @param array $path // 文件路径
* @return array
* @author: BillZhang <708947679@qq.com>
* @Date: 2019/10/8 3:30 PM
*/
function fileList($dirPath,$path=[]){
//1、首先先读取文件夹
$temp=scandir($dirPath);
//遍历文件夹
foreach($temp as $v){
if($v!=null){
$filePath=$dirPath.$v;
if(strpos($filePath,'.htm') !== false){ // 筛选文件的关键字
if(is_dir($filePath)){//如果是文件夹则执行
if($v=='.' || $v=='..'){//判断是否为系统隐藏的文件.和.. 如果是则跳过否则就继续往下走,防止无限循环再这里。
continue;
}
$path[] = $filePath;
fileList($filePath);//因为是文件夹所以再次调用自己这个函数,把这个文件夹下的文件遍历出来
}else{
$path[] = $filePath;
}
}
}
}
return $path;
}