tags:

views:

67

answers:

1

I'm looking for a regex that will recognize # followed by a number in a string and make it clickable. Only if its # and a number like ex: #758 and make i clickable. Not # 758. Youtube has this for example.

Would really appreciate if someone could give me some hints since im worthless on regex.

+1  A: 

Try this:

preg_replace('/#\\d+/', '<a href="$0">$0</a>', $str);

The regex is basically /#\d+/, so the # character followed by one or more digits. preg_replace is to replace such occurences with <a href="$0">$0</a> where $0 is replaced by the match that has been found.

And if you just need the number, use /#(\d+)/ and <a href="$1">$1</a> instead.

Gumbo
@Gumbo: does $0 contain the whole match?
Renesis
@Renesis: Yes, `$0` contains the whole match.
Gumbo
that worked exactly how i wanted it! thanks!