Adam Baney
Adam Baney

Reputation: 57

Use PHP to Convert Email Text to a Link

Based on a question I had answered here ( Use PHP to Replace HTML with HTML ), I'd like to be able to filter my output text for email addresses, and convert those text emails to "mailto" links.

Here's the PHP code that works, but only for converting some HTML to other HTML. What I've tried to do is have this function look for an email address, and convert it to a "mailto" link. For whatever reason, the code doesn't convert the email addresses. Here's my PHP:

function text_filter($string) {
    $search  = array('<p>__</p>',   '/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/');
    $replace = array('<hr />',      '<a href="mailto:$2">$2</a>');
    $processed_string = str_replace($search, $replace, $string);
    echo $processed_string;
}

When I use this function for output, this is what the code looks like:

<?php text_filter( get_the_content() ); ?>

Upvotes: 1

Views: 5773

Answers (6)

Martin Zvar&#237;k
Martin Zvar&#237;k

Reputation: 2479

@Adam Baney - This will work even when used repeatedly.

// EMAILS
$str =  preg_replace('~(^|[\s\.,;\n\(])([a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})~',
  '$1<a href="mailto:$2">$2</a>',
  $str);

// PHONE NUMBERS
$str =  preg_replace_callback('~(^|[\s\.,;\n\(])(?<! )([0-9 \+\(\)]{9,})~', function($m) {
  return $m[1].'<a href="tel:'.preg_replace('~[^0-9\+]~', '', $m[2]).'">'.$m[2].'</a>';
}, $str);

Upvotes: 1

Soichi Hayashi
Soichi Hayashi

Reputation: 3584

Here is another version of this that seems to work for me. I've added + char to handle "plus addressing" (like [email protected])

function replaceemail($text) {-
    $ex = "/([a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/";
    preg_match_all($ex, $text, $url);
    foreach($url[0] as $k=>$v) $text = str_replace($url[0][$k], '<a href="mailto:'.$url[0][$k].'" target="_blank" rel="nofollow">'.$url[0][$k].'</a>', $text);
    return $text;
}

Upvotes: 0

Evalds Urtans
Evalds Urtans

Reputation: 6694

Another way to do it in order so that it would work with existing html links in text:

function html_parse_text($text)
{    
    $text = preg_replace("/(?<!\")(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/",
            '<a href="\\1" target=_blank>\\1</a>', $text);       
    $text = preg_replace("/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/",
            '\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
    $text = preg_replace("/(?<!\")([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/",
            '<a href="mailto:\\1" target=_blank>\\1</a>', $text);

    return $text;
}

Upvotes: 0

zetanova
zetanova

Reputation: 481

function obfuscate_email($content){

    $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
    $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
    $replacement = '<a href="mailto:\\1">\\1</a>';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;

}

And add filter

add_filter( 'the_content', 'obfuscate_email' );

Upvotes: 0

Sammitch
Sammitch

Reputation: 32272

  1. str_replace() doesn't use regular expressions, rewritten with preg_replace().
  2. Added delimiters to first matching expression.
  3. Fixed replacement from $1 to $2.

 

function text_filter($string) {
    $search  = array('/<p>__<\/p>/', '/([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/');
    $replace = array('<hr />', '<a href="mailto:$1">$1</a>');
    $processed_string = preg_replace($search, $replace, $string);
    echo $processed_string;
}

Upvotes: 2

Luceos
Luceos

Reputation: 6730

You can't use str_replace to do a regular expression replace.

You will need to split the actions up.

function text_filter($string) {
    $search  = array('<p>__</p>');
    $replace = array('<hr />');
    $processed_string = str_replace($search, $replace, $string);
$processed_string = preg_replace('/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/','<a href="mailto:$2">$2</a>',$processed_string);
    echo $processed_string;
}

See: http://www.php.net/manual/en/function.preg-replace.php for preg replacing.

Upvotes: 0

Related Questions