Grow your CSS skills. Land your dream job.

Last updated on:

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. 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. Permalink to comment#

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

  3. 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. 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. 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. Permalink to comment#

    Actualy 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);

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".