Skip to content

Instantly share code, notes, and snippets.

@georgioupanayiotis
Last active August 29, 2015 14:14
Show Gist options
  • Save georgioupanayiotis/ffdfaf6ec1875b1cd199 to your computer and use it in GitHub Desktop.
Save georgioupanayiotis/ffdfaf6ec1875b1cd199 to your computer and use it in GitHub Desktop.
Convert an email into link from text in PHP
$string = 'please contact me on email@emaildomain.com';
ereg_replace("[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})", "<a href=\"mailto:\\0\">\\0</a>", $string);
ereg_replace is deprecated therefore we can use:
function convertEmailToLinks($string) {
return preg_replace("/[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})/", "<a href=\"mailto:\\0\">\\0</a>", $string);
}
Automatically converts into a link, since ereg_replace() is deprecated in PHP 5.3 we could use preg_replace
Now you can use above function like below:
echo convertEmailToLinks($string);
And output will be:
please contact me on <a href="mailto:email@emaildomain.com">email@emaildomain.com</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment