Automatically create email link from a static text
16,395
Solution 1
You will need to use regex:
<?php
function emailize($text)
{
$regex = '/(\S+@\S+\.\S+)/';
$replace = '<a href="mailto:$1">$1</a>';
return preg_replace($regex, $replace, $text);
}
echo emailize ("bla bla bla [email protected] bla bla bla");
?>
Using the above function on sample text below:
blalajdudjd [email protected] djjdjd
will be turned into the following:
blalalbla <a href="mailto:[email protected]">[email protected]</a> djjdjd
Solution 2
Try this version:
function automail($str){
//Detect and create email
$mail_pattern = "/([A-z0-9_-]+\@[A-z0-9_-]+\.)([A-z0-9\_\-\.]{1,}[A-z])/";
$str = preg_replace($mail_pattern, '<a href="mailto:$1$2">$1$2</a>', $str);
return $str;
}
Update 31/10/2015: Fix for email address like [email protected]
function detectEmail($str)
{
//Detect and create email
$mail_pattern = "/([A-z0-9\._-]+\@[A-z0-9_-]+\.)([A-z0-9\_\-\.]{1,}[A-z])/";
$str = preg_replace($mail_pattern, '<a href="mailto:$1$2">$1$2</a>', $str);
return $str;
}
Author by
Raffaele
Updated on July 27, 2022Comments
-
Raffaele almost 2 years
I'm trying to figure out how to automatically link the email addresses contained in a simple text from the db when it's printed in the page, using php.
Example, now I have:
Lorem ipsum dolor [email protected] sit amet
And I would like to convert it (on the fly) to:
Lorem ipsum dolor <a href="mailto:[email protected]">[email protected]</a> sit amet
-
darjab about 15 yearsYour before conversion is exactly the same as your after conversion. Please, explain a little more what you need.
-
-
dusoft about 15 yearsno, he probably needs regex to find and replace any emails in the text.
-
Raffaele about 15 yearsexactly, convert the email address contained in the text to a "mailto" link with the surrounding text intact
-
Raffaele about 15 yearsthanks, I tried but it seem to not work. I've passed the text via "$text" and put an "echo $result;" after the } but nothing come out. Maybe I'm doing a stupid error, I'm just started to learn php.
-
Erick about 15 yearsYup, I tested it and didn't worked at first. I just retested it. Now it works like a charm. If youw ant to see the ouput of the regex just comment out the preg_replace / return part. Otherwise the preg_match/var_dump wont be displayed ;-)
-
Felix Eve over 11 yearsNot quite working right, my email link is coming out like this: <a href="mailto:[email protected]</div></div></div>" >[email protected]</a>
-
GuruBob almost 11 yearsExcellent - works well. How you would prevent it being encoded again if the code is reapplied to the output string. In my case I would like to run this code over some HTML before saving it in the database, but the code that has been fetched from the database may already have mailto: linked email addresses. I end up with:
Bob Brown <a <a href="mailto:href="mailto:[email protected]">[email protected]o.nz</a>">href="mailto:[email protected]z">[email protected]z</a></a>
-
rodrigoq almost 11 yearsIt doesn't work with
[email protected].
This regex works better:'/([a-zA-Z0-9_\-\.]*@\S+\.\w+)/'
-
Chris Andersson over 9 yearsThis should be up voted instead of the answer marked as correct, this works much better.
-
hbit over 8 yearsDoes not work for email addresses ala [email protected]
-
mdikici over 7 yearsThis works if there is space just after e-mail address. But doesn't work in this situation: [email protected]<br>Test... @NotthingCtrl 's answer is better.
-
Ogugua Belonwu about 7 years@NotthingCtrl , does this ignore emails that are already linked?
-
Ismail almost 7 years@OguguaBelonwu nope, it will match them as well. Will fall for
http://example.com/email/[email protected]/
as well, needs couple tweaks. -
tom f almost 4 yearscan confirm this works better --- the accepted answer works but it includes stuff like punctuation, etc, if it comes right after the email. So for example: "Send an email to [email protected], or call" produces a match as
[email protected],
. this solves that issue