Automatic Mailto Links

$string = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})','<a href="mailto:\\1">\\1</a>', $text);
echo $string;

Comments

  1. User Avatar
    AndiD
    Permalink to comment#

    Keep in mind that the POSIX Regex functions are deprecated in PHP 5.3.0 and removed in PHP 6.

    See http://us2.php.net/manual/en/function.eregi-replace.php

  2. User Avatar
    Frank
    Permalink to comment#

    AndiD is right. Use preg_* functions instead of eregi_* functions.

  3. User Avatar
    Attila Hajzer
    Permalink to comment#

    what does this do though? im thinking about making a mail:to link on the footer of my site :

    http://attilahajzer.host-ed.net/

    take a look and see if it’d be worth it.

  4. User Avatar
    Soladnet
    Permalink to comment#

    Alternatively since eregi_replace is deprecated use preg_replace thus

    $stringa = “This should format my email address example@domain.com“;

    $pattern = “/([a-z0-9][_a-z0-9.-]+@([0-9a-z][_0-9a-z-]+\.)+[a-z]{2,6})/i”;
    $replace = “\\1“;
    $text = preg_replace($pattern, $replace, $stringa);
    echo htmlspecialchars($text);

  5. User Avatar
    ljasinskipl
    Permalink to comment#

    Actually it should be something like
    $pattern = “/([a-z0-9][_a-z0-9.-]+@([0-9a-z][_0-9a-z-]+\.)+[a-z]{2,6})/i”;
    $replace = ‘\\1‘;
    $text = preg_replace($pattern, $replace, $stringa);

  6. User Avatar
    ljasinskipl
    Permalink to comment#

    Actualy it should be something like

    $pattern = "https://cdn.css-tricks.com/([a-z0-9][_a-z0-9.-]+@([0-9a-z][_0-9a-z-]+\.)+[a-z]{2,6})/i";
    $replace = '\\1';
    $text = preg_replace($pattern, $replace, $stringa);

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag