$text = "14& this is a reply to the comment with id 14";
var_dump(preg_replace("~\d+&~", '<u>$0</u>', $text));
输出:
string '<u>14&</u> this is a reply to the comment with id 14' (length=52)
为了摆脱&
的:
preg_replace("~(\d+)&~", '<u>$1</u>', $text)
输出:
string '<u>14</u> this is a reply to the comment with id 14' (length=51)
$0
或$1
会你的ID。你可以用你喜欢的任何东西来替换标记。
例如链接:
$text = "14& is a reply to the comment with id 14";
var_dump(preg_replace("~(\d+)&~", '<a href="#comment$1">this</a>', $text));
输出:
string '<a href="#comment14">this</a> is a reply to the comment with id 14' (length=66)
空间,任何规模的数量和空间,然后 - 是正确的? – 2012-03-25 21:45:29
@Dagon是的,这是正确的。 :) – 2012-03-25 21:46:03
你是否希望它也取代3和4在下面,或者应该被排除? '1&开头。 2&在中间。 3和后面跟着一个词。 And4&前面有一个词。最后以5'结尾? – 2012-03-25 22:05:10