Hashtag and Username Linking in Twitter Apps
Are you trying to add automatic hashtag and username hot linking in your PHP twitter app?
I just tried to do this with my amdavidson.me lifestreaming site. It’s just two nested regular expressions. If there’s some problem with nested preg_replace functions, or if any regular expression geniuses have better ideas, feel free to comment.
But if not, here’s a little code for you, should make things super easy. It assumes your tweet variable is $tweet, replace with whatever you’d need.
1 2 3 4 5 | echo preg_replace('/\#([a-zA-Z0-9_]{1,15})?/', "<a href=\"http://hashtags.org/tag/$1\">#$1</a>", preg_replace('/\@([a-zA-Z0-9_]{1,15})?/', "<a href=\"http://twitter.com/$1\">@$1</a>", $tweet)); |
Tags: amdavidson.me, php, snippet, twitter
