在两行之间替换文本

在两行之间替换文本

问题描述:

我一直在尝试将两个符号之间的文本替换为preg_replace,但是可惜仍然无法完全正确,因为我得到的是空字符串,这是到目前为止的结果>

I have been trying to get the text between two symbols to be replaced with preg_replace, but alas still not quite getting it right as I get a null output that is empty string, this is what I have so far

$start = '["';
$end   = '"]';
$msg   = preg_replace('#('.$start.')(.*)('.$end.')#si', '$1 test $3', $row['body']);

所以我要寻找的示例输出将是:

So an example output I am looking for would be:

normal text [everythingheregone] after text 

收件人

 normal text [test] after text

您正在将$ start和$ end定义为数组,但将其用作普通变量.尝试将代码更改为此:

You are defining $start and $end as arrays, but using it as normal variables. Try changing your code to this:

$start = '\[';
$end  = '\]';
$msg = preg_replace('#('.$start.')(.*)('.$end.')#si', '$1 test $3', $row['body']);