This is a simple PHP snippet which will turn a twitter username into a link. This technique requires a regular expression function called preg_replace().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
  // Setup the example string
  $text = 'Am example string posted by @stuarteske';
  // Apply the regular expression
  $text = preg_replace(
    '/@(\w+)/',
    '<a href="http://twitter.com/$1">@$1</a> ',
    $text
  );//turn twitter usernames into links
  // Trace the string
  print $text;
  // Ouput String:
  // Am example string posted by
  //    <a href="http://twitter.com/stuarteske">@stuarteske</a>
?>