douren2395 2014-06-22 00:31
浏览 41
已采纳

在PHP中删除文本文件的未知内容

Is there a way to remove contents (substring) of a text file in PHP.

I know the start and the end of the string to remove, but not everything in between the content

File (routes.php) I want to edit

I want to keep this text here
# [[[@-100] I also want to remove the comment line here
  I want to remove this text here
# I also want to remove this line  [@-100]]]
I want to keep this line too

So I want to remove the content block that starts with # [[[@-100] and ends with [@-100]]]

Im using codeignighter so I write my file like so

$ROUTE_FILE ='system/routes.php'

$output = file_get_contents($ROUTE_FILE);

$output = $output->remove_substring(...);

$this->write_file($ROUTE_FILE, $output);
  • 写回答

2条回答 默认 最新

  • drutcs7210 2014-06-22 00:50
    关注

    Tested:

    $mystring='this is # [[[@-100] not [@-100]]] this string you want';
    
    echo remove_substring('# [[[@-100]','[@-100]]]',$mystring);
    
    function remove_substring($head,$foot,$string){
        $top=reset(explode($head,$string));
        $bottom=end(explode($foot,$string));
        return $top.$bottom;
    }
    

    output:

    this is this string you want

    Here's a version that does not return errors:

    Tested:

    $mystring='this is # [[[@-100] not [@-100]]] this string you want';
    
    echo remove_substring('# [[[@-100]','[@-100]]]',$mystring);
    
    function remove_substring($head,$foot,$string){
        $top=explode($head,$string);
        $top=reset($top);
        $bottom=explode($foot,$string);
        $bottom=end($bottom);
        return $top.$bottom;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错