Twitter Feed Regular Expressions
I am trying to use regular expressions to add links in the appropriate places (regular links, hashtags, @ replies) in a Twitter RSS feed I have on my blog.
Currently, I am using the following functions:
$text = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@i', '<a href="$1">$1</a>', $text); /* URLs */
$text = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/','$1<a href="http://twitter.com/$2">@$2</a>',$text); /* @replies */
$text = preg_replace('/([^a-zA-Z0-9-_&])#([0-9a-zA-Z_]+)/',"$1<a href="http://twitter.com/search?q=%23$2">#$2</a>",$text); /* hashtags */
The functions work so long as there is not a hashtag or @ reply at the beginning of the tweet. A link is not applied to hashtags or @ replies in this position. For regular links in this position, a link is properly applied.
Let me know if you have any questions, and thanks in advance for your assistance!
- The topic ‘Twitter Feed Regular Expressions’ is closed to new replies.