Skip to content

Instantly share code, notes, and snippets.

@joshhartman
Created February 20, 2011 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshhartman/836352 to your computer and use it in GitHub Desktop.
Save joshhartman/836352 to your computer and use it in GitHub Desktop.
Automatically Link URLs and Email Addresses Within Text
<?php
function linkify($text) {
$text = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[A-Z0-9+&@#\/%=~_|]/i', '<a href="\0">\0</a>', $text);
$text = preg_replace('/\b((mailto:)?[A-Z0-9._%+-]+@[A-Z0-9._%-]+\.[A-Z]{2,4})/i', '<a href="mailto:\0">\0</a>', $text);
return $text;
}
$test_text = "An example of an email address is someone@example.com. Google (http://www.google.com) is the leading search engine! Check out the latest tut @ http://nettuts.com.";
echo linkify($test_text);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment