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;
}
Share:
16,395
Raffaele
Author by

Raffaele

Updated on July 27, 2022

Comments

  • Raffaele
    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
      darjab about 15 years
      Your before conversion is exactly the same as your after conversion. Please, explain a little more what you need.
  • dusoft
    dusoft about 15 years
    no, he probably needs regex to find and replace any emails in the text.
  • Raffaele
    Raffaele about 15 years
    exactly, convert the email address contained in the text to a "mailto" link with the surrounding text intact
  • Raffaele
    Raffaele about 15 years
    thanks, 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
    Erick about 15 years
    Yup, 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
    Felix Eve over 11 years
    Not quite working right, my email link is coming out like this: <a href="mailto:[email protected]</div></div></div>" >[email protected]</a>
  • GuruBob
    GuruBob almost 11 years
    Excellent - 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="mail‌​to:[email protected]‌​z">[email protected]‌​z</a></a>
  • rodrigoq
    rodrigoq almost 11 years
    It doesn't work with [email protected]. This regex works better: '/([a-zA-Z0-9_\-\.]*@\S+\.\w+)/'
  • Chris Andersson
    Chris Andersson over 9 years
    This should be up voted instead of the answer marked as correct, this works much better.
  • hbit
    hbit over 8 years
    Does not work for email addresses ala [email protected]
  • mdikici
    mdikici over 7 years
    This 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
    Ogugua Belonwu about 7 years
    @NotthingCtrl , does this ignore emails that are already linked?
  • Ismail
    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
    tom f almost 4 years
    can 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