user1228907 user1228907 - 5 months ago 10
PHP Question

PHP preg_replace match numbers following a special character

I'm creating a comment board feature that allows users to reference post-ID's, which will be auto-configured by regex to hyperlink to the relevant post.

Posts references are formatted as the following, using the double-arrow ASCII symbol:

»1234


6 numbers maximum can follow the double-arrow in order for the reference to be hyperlinked, so
»1234567
would not hyperlink, but
»1
,
»12
,
»123
, etc would.

How would I go about doing this with regex?

Answer

Match the special character followed by 1-6 digits and then followed by a word boundary, so it won't match if it's concatenated with any other string.

»\d{1,6}\b