$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
будет вашим идентификатором. Вы можете заменить разметку на что угодно.
например ссылка:
$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)