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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度