PHP批量替换指定目录下静态文件中的字符

        当网站生成了很多静态文件的时候,有时候要进行替换要不就全部重新生成,如果后台系统有问题或不能生成就只能批量替换比较文件。

        下面发布一个可以批量替换的类,不依赖任何框架可直接运行。

<?php
// +----------------------------------------------------------------------
// | HNZHISHENG.NET [ Beautiful and practical ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://www.hnzhisheng.net All rights reserved.
// | DateTime: 2022/1/12 15:32
// +----------------------------------------------------------------------
// | Author: hotlinhao <duyuebing@126.com>
// | Created by PhpStorm. Dyb
// | description:
// +----------------------------------------------------------------------

error_reporting(E_ALL);


//要查找的目录
const SEARCH_PATH = 'E:\****\public';

//查找的字符串
const SEARCH_ARR = [
    'http://site.***.com.cn',
    'http://attr.***.com.cn',
    'http://news.***.com.cn',
    'http://s94.cnzz.com/'
];
//替换成的字符串
const REPLACE_ARR = [
    'https://site.***.com.cn',
    'https://attr.***.com.cn',
    'https://news.***.com.cn',
    'https://s94.cnzz.com/'
];

//替换的文件类型
const REL_EXT = ['html'];

$ds = DIRECTORY_SEPARATOR;

$fileIndex = 0;
$step = 0;

function replacePath($path){
    global $ds,$fileIndex,$step;
    if(!is_dir($path)){
        return false;
    }
    $handle = @opendir($path);

    while (false !== ($file = readdir($handle))){
        if(is_dir($path . $ds . $file) && $file != '.' && $file != '..'){
            replacePath($path . $ds . $file);
        }else{
            if($file != '.' && $file != '..'){
                //echo $file;
                $info = pathinfo($file);
                if(in_array($info['extension'],REL_EXT)){
                    if($step = 500){
                        echo 'file Count:'.$fileIndex ."\n";
                        sleep(2);
                        $step = 0;
                    }
                    $step++;
                    $fileIndex++;
                    replaceFile($path . $ds . $file);
                }

            }
        }
    }
    closedir($handle);
}

function replaceFile($file_path){
    $content = file_get_contents($file_path);
    //检查有没有要替换的字符,如果没有直接跳过
    $hasStr = false;
    foreach (SEARCH_ARR as $sItem){
        if(strpos($content,$sItem) !== false){
            $hasStr = true;
            break;
        }
    }
    if($hasStr === false){
        unset($content);
        return true;
    }
    $content = str_replace(SEARCH_ARR,REPLACE_ARR,$content);
    file_put_contents($file_path,$content);
    unset($content);
    return true;
}

$startTime = time();
echo "Start Replace:\n";
replacePath(SEARCH_PATH);

$endTime = time();

$totalSecond = $endTime - $startTime;
$minute = 0;
$modVal = 0;

if($totalSecond > 0){
    $minute = floor($totalSecond/60);
    $modVal = $totalSecond%60;
}

echo 'end, file total:'. $fileIndex .' use time:'. $minute .' minute '.$modVal ."second\n";

另存为一个文件,建议使用命令行运行。

命令行支持方式: php 路径/filename.php

如下图:

 如果你的php没有加入环境变量,需要填完整的php.exe路径。

如何查看PHP有没有在环境变量中。在命令行输入php -v 

如果有版本信息输出,则是在环境变量。否则需要加入或输入php.exe全路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值