Last active
August 29, 2015 14:14
-
-
Save georgioupanayiotis/ffdfaf6ec1875b1cd199 to your computer and use it in GitHub Desktop.
Convert an email into link from text in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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