【问题标题】:How to add or replace string between specific string?如何在特定字符串之间添加或替换字符串?
【发布时间】:2015-06-16 12:08:21
【问题描述】:

我需要使用 PHP 在特定示例之间的文本文件中替换或添加新文本。

在文本文件中[自动更新文本]旧字符串[/结束自动更新文本]之间的区域添加新字符串 p>

【问题讨论】:

    标签: php search text replace add


    【解决方案1】:

    快速而肮脏的方式是这样的

    $text = "[auto update text][/end auto update text]";
    
    $text = preg_replace('/(?<=\[auto update text\]).*?(?=\[\/end auto update text\])/', 'Thing to add', $text);
    
    echo $text; 
    

    输出是[auto update text]Thing to add[/end auto update text]

    对于 BBCode 风格的约定,您可能应该将标签改为 [autoupdate][/autoupdate]

    【讨论】:

    • @grudphunsanit 很高兴听到这个消息!如果您发现答案有帮助,请考虑投票并用复选标记标记此答案。谢谢。 :)
    猜你喜欢
    • 2015-03-13
    • 1970-01-01
    • 2012-09-04
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    相关资源
    最近更新 更多