Skip to content

Instantly share code, notes, and snippets.

@shyamsalimkumar
Last active December 18, 2015 08:18
Show Gist options
  • Save shyamsalimkumar/5752537 to your computer and use it in GitHub Desktop.
Save shyamsalimkumar/5752537 to your computer and use it in GitHub Desktop.
PHP - Convert URL in text
function convertLinks($text) {
$text = preg_replace('/(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9@:;%_\+.~#?&\/\/=]+)/', '<a href="\\1" target="_blank">\\1</a>', $text);
$text = preg_replace('/([[:space:]()[{}])(www.[-a-zA-Z0-9@:;%_\+.~#?&\/\/=]+)/', '\\1<a href="http://\\2" target="_blank">\\2</a>', $text);
$text = preg_replace('/(([0-9a-zA-Z\.\-\_]+)@([0-9a-zA-Z\.\-\_]+)\.([0-9a-zA-Z\.\-\_]+))/', '<a href="mailto:$1">$1</a>', $text);
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment