Twitter Feed Regular Expressions (2 posts)

  1. Tom Lany
    Posted 6 years ago #


    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!

  2. Tom Lany
    Posted 6 years ago #

    I was able to find a great article with expressions that work for this purpose: http://www.snipe.net/2009/09/php-twitter-clickable-links/

Topic Closed

This topic has been closed to new replies.

About this Topic