在PHP中使用正则表达式捕获时如何忽略字符串中的单词?

Here is what my code looks like:

preg_replace("/(test)/i", "<strong>$1</strong>", $text);

And here is an example string:

"<a href="www.website.com/search/%23test">#test</a>"

I want to capture the second 'test' but not the first one in the URL.

You can avoid matching something which is inside angular brackets (at least, if there are no literal angular brackets in the text) with a negative lookahead:

preg_replace("/test(?![^<>]*>)/i", "<strong>$0</strong>", $text);

regex101 demo