-2

I'm using preg_replace() as in the following code.

$p = preg_replace('/#[\d\w]+/', '<a href="http://'.$_SERVER['HTTP_HOST'].'/search/term:'.str_replace('#', rawurlencode('%23'), '${1}').'">${1}</a>', $p);
$p will be a string like '(#ben)'

However, nothing is output using $1. Am I using $1 incorrectly?

Should output this: <h2>Bens (<a href="http://example.com/search/term:#ben">ben</a>) cat</h2>

4

1 回答 1

6

您需要一个捕获组来引用反向引用$1

preg_replace('/(#[\d\w]+)/', ... 
               ^        ^

看到它在这个演示中工作。

于 2012-09-13T13:19:00.850 回答