Jacob Tomlinson's profile picture Jacob Tomlinson
Home Blog Talks About

Convert tweet hashtags, at-tags and urls to links with PHP and Regular Expressions

2 minute read #guide, #php, #regular-expressions, #tutorial, #tweet, #twitter-api

Now of course if you’re using the Twitter API you can use Twitter entities but in this tutorial we’re going to use regular expressions.

What are Regular Expressions?

So you may be wondering what regular expressions or regex are. Basically they are a very powerful search and replace feature implemented in almost every modern programming language for manipulating strings. Now we’re not going to get into how regular expressions work here but if you want to know more then see this useful tool.

In PHP

So to make use of regular expressions in PHP you will use the function preg_replace(), this function must be given 3 parameters; a search string written in regex, a replacement string written in regex and then the string you are manipulating.

So what we’re going to do is to search for hashtags, at-tags and urls in turn and replace each with an HTML anchor tag pointing to the correct url.

$tweet = "@george check out http://www.google.co.uk #google";

//Convert urls to <a> links
$tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);

//Convert hashtags to twitter searches in <a> links
$tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);

//Convert attags to twitter profiles in &lt;a&gt; links
$tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);

echo $tweet;

Which gives the output

Now this is all well and good but you don’t want to be implementing this code every time you want to “linkify” a tweet so lets wrap it up in a function which you can put at the top of your code or in an included module.

function linkify_tweet($tweet) {

  //Convert urls to <a> links
  $tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);

  //Convert hashtags to twitter searches in <a> links
  $tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);

  //Convert attags to twitter profiles in <a> links
  $tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);

  return $tweet;

}

So now we can simply “linkify” our tweets with the following code.

$tweet = "@george check out http://www.google.co.uk #google";

echo linkify_tweet($tweet);

Have thoughts?

I love hearing feedback on my posts. You should head over to Twitter and let me know what you think!

Spotted a mistake? Why not suggest an edit!