-1

如果我有这样的文本字符串:

In the beginning<WH7225> God<WH430> created<WH1254><WH853> the heaven<WH8064> and<WH853> the earth<WH776>.

我想用包含 H 和以下数字的链接替换标签 <>,我该如何用 PHP 做到这一点?

4

3 回答 3

1

在您的搜索正则表达式(preg_replace如:第一个括号)。H[0-9]+<W(H[0-9]+)>$1

于 2013-10-23T21:30:19.623 回答
0

这应该有效:

$s = 'In the beginning<WH7225> God<WH430> created<WH1254><WH853> the heaven<WH8064> and<WH853> the earth<WH776>.';

$s = preg_replace('~<(WH\d+)>~', '<a href="mysite.php?code=$1">$1</a>', $s);

输出:

In the beginning<a href="mysite.php?code=WH7225">WH7225</a> God<a href="mysite.php?code=WH430">WH430</a> created<a href="mysite.php?code=WH1254">WH1254</a><a href="mysite.php?code=WH853">WH853</a> the heaven<a href="mysite.php?code=WH8064">WH8064</a> and<a href="mysite.php?code=WH853">WH853</a> the earth<a href="mysite.php?code=WH776">WH776</a>.

于 2013-10-23T21:31:15.133 回答
0

对于这个例子:

$string = preg_replace('/<(WH[^>]+)>/', '<a href="mysite.php?code=$1">$1</a>', $string);
于 2013-10-23T21:30:10.210 回答