如何在特定字符串之间添加或替换字符串?

How to add or replace string between specific string?

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

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

一种快速而肮脏的方法是这样的

$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]