如何从 preg_replace 结果中删除模式内容?

How to remove pattern stuffs from preg_replace results?

我有这个代码:

$text='This [word] should be a link';
echo preg_replace('/\[\w+\]/', "<a href='#'>[=11=]</a>", $text);

输出:

This <a href='#'>[word]</a> should be a link

我怎样才能得到这个输出:

This <a href='#'>word</a> should be a link

使用捕获组并使用 </code> 替换:</p> <pre><code>echo preg_replace('/\[(\w+)\]/', "<a href='#'></a>", $text); This <a href='#'>word</a> should be a link