]> Convert URLs to links automatically with PHP 🌐:aligrant.com

Convert URLs to links automatically with PHP

Alastair Grant | Thu 19 Jul 2012

Lots of blogs and things about this kicking around - many complicated, many just don't work. This seemed to do the trick for me:

public static function ParseURL($text) {
  $text = preg_replace('`www.S+`', 'http://{0}', $text);
  $text = preg_replace('`[a-z]{3,6}://S+.S*`', '$0', $text);
  return $text;
}

public static function ParseEmail($text) {
  return preg_replace('`[w.-]+@w+[w.-]*?.w{1,4}`im', '$0', $text);
}
Breaking from the voyeuristic norms of the Internet, any comments can be made in private by contacting me.

Related