我有这段代码:
$text='This [word] should be a link';
echo preg_replace('/\[\w+\]/', "<a href='#'>$0</a>", $text);
输出:
This <a href='#'>[word]</a> should be a link
我如何获得此输出:
This <a href='#'>word</a> should be a link
答案 0 :(得分:0)
使用捕获组并替换$1
:
echo preg_replace('/\[(\w+)\]/', "<a href='#'>$1</a>", $text);
This <a href='#'>word</a> should be a link