PHP文件操作 - 替换某行,插入某行,删除某行,获取行号

    #在需要查找的内容后一行新起一行插入内容,
    #$position
    #           start表示在第一次出现的位置插入,
    #           end表示在最后一次出现的位置插入
    function insertAfterTargetNewLine($filePath, $insertCont, $target,$position = 'start')
    {
        $result = null;
        $count = 1;
        $targetIndex = null;
        $fileCont = file_get_contents($filePath);
        if('start' == $position){//第一次出现的地方传入
            $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标
        }
        else if("end" == $position){//最后一次出现的地方传入
            $targetIndex = strrpos($fileCont, $target); #查找目标字符串的坐标
        }
        #找到target的后一个换行符
        $chLineIndex = strpos(substr($fileCont, $targetIndex), PHP_EOL) + $targetIndex;
        if ($chLineIndex !== false) {
            #插入需要插入的内容
            $fileCont = substr($fileCont, 0, $chLineIndex + 1) . $insertCont . PHP_EOL . substr($fileCont, $chLineIndex + 1);
        }

        file_put_contents($filePath, $fileCont);


    }
    #在需要查找的内容后插入内容
    function insertAfterTarget($filePath, $insertCont, $target)
    {
        $result = null;
        $fileCont = file_get_contents($filePath);
        $count = substr_count($fileCont,$target);//查询子串出现的次数
        echo $count;
        if($count > 0){
            for($i=0;$i<$count;$i++){
                $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标
                if ($targetIndex !== false) {
                    #插入需要插入的内容
                    $result = substr_replace($fileCont,$insertCont,$targetIndex+strlen($target),0);//起始位置,0表示插入
                    file_put_contents($filePath, $result);
                }
            }
            file_put_contents($filePath, $fileCont);
        }else{
            return false;
        }
    }
    #替换指定字符串,或删除指定字符串
    function replaceTarget($filePath,$target,$replaceCont='')
    {
        $result = null;
        $fileCont = file_get_contents($filePath);
        $count = substr_count($fileCont,$target);//查询子串出现的次数
        echo $count;
        if($count > 0){
            for($i=0;$i<$count;$i++){
                $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标
                if ($targetIndex !== false) {
                    #替换指定行
                    $fileCont = substr_replace($fileCont,$replaceCont,$targetIndex,strlen($target));
                }
            }
            file_put_contents($filePath, $fileCont);
        }else{
            return false;
        }

    }
    #删除内容所在的某一行
    function delTargetLine($filePath, $target)
    {
        $result = null;
        $fileCont = file_get_contents($filePath);

        $count = substr_count($fileCont,$target);//查询子串出现的次数
        echo $count;
        if($count > 0){
            for($i=0;$i<$count;$i++){
                $targetIndex = strpos($fileCont, $target); #查找目标字符串的坐标

                if ($targetIndex !== false) {
                    #找到target的前一个换行符
                    $preChLineIndex = strrpos(substr($fileCont, 0, $targetIndex + 1), PHP_EOL);
                    #找到target的后一个换行符
                    $AfterChLineIndex = strpos(substr($fileCont, $targetIndex), PHP_EOL) + $targetIndex;
                    if ($preChLineIndex !== false && $AfterChLineIndex !== false) {
                        #重新写入删掉指定行后的内容
                        $fileCont = substr($fileCont, 0, $preChLineIndex + 1) . substr($fileCont, $AfterChLineIndex + 1);
                    }
                }
            }
            file_put_contents($filePath, $fileCont);
        }else{
            return false;
        }
    }

#获取某段内容的行号
    /**
     * @param $filePath
     * @param $target   待查找字段
     * @param bool $first   是否再匹配到第一个字段后退出
     * @return array
     */
    function getLineNum($filePath, $target, $first = false)
    {
        $fp = fopen($filePath, "r");
        $lineNumArr = array();
        $lineNum = 0;
        while (!feof($fp)) {
            $lineNum++;
            $lineCont = fgets($fp);
            if (strstr($lineCont, $target)) {
                if($first) {
                    return $lineNum;
                } else {
                    $lineNumArr[] = $lineNum;
                }
            }
        }
        return $lineNumArr;
    }
    public function index()
    {
        $info=[];
//        $result = $this->getNetworkInfo("cat /etc/resolv.conf");
        $target = "127.1.1.1 www";
        $filePath = "/etc/hostsbk";
        //        文件结尾追加字符串
//        echo file_put_contents("/etc/hostsbk",$target.PHP_EOL,FILE_APPEND|LOCK_EX);
//        删除字符串
        $result = $this->delTargetLine($filePath,$target);
        dump($result);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

InterestingFigure

迈克 Let's Go

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值