This is a simple PHP snippet which will turn a URL 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
<?php
  // Setup the example string
  $text = 'Am example string containing a URL http://stuarteske.com/';
  // Apply the regular expression
  $text = preg_replace(
    '@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@',
    '<a href="$1">$1</a> ', 
    $text
  ); //turn URLs into links
  // Trace the string
  print $text;
?>