Jump to content

Regex email to active link


tomerg3

Recommended Posts

I am using this code to convert a [email protected] to < a href="mailto: ..">

 

I want to modify it so if there's also an active link < a href="mailto:...> in the text, it will not be changed

 

My current code is:

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

 

Help please.

Link to comment
https://forums.phpfreaks.com/topic/60607-regex-email-to-active-link/
Share on other sites

One solution is to remove the already-linked e-mail addresses, then replace what's left.  I do this by replacing them with a special piece of bookmark code containing the index of the array where I've saved them, replace the remaining e-mails, and then return the "stored" strings to their places.

 

<?php

function _pcre_emails ($txt,&$array_ref,&$counter_ref) { $array_ref[$counter_ref] = str_replace('\"','"',$txt); return '<?php' . $counter_ref++ . '?' . '>'; }
// (Separated ? and > above so PHP highlighting would work, but not necessary in code)
$emails = array();
$email_counter = 0;

$text = 'I am using this code to convert a [email protected] to <a href="mailto:[email protected]">[email protected]</a>';
echo "\n",htmlentities($text),"\n";
echo htmlentities(preg_replace(
array(
	'/(<a href="mailto:.*?">.*?<\/a>)/ie',
	'/([\w.-]+@(?:[\w-]+\.){1,3}\w{2,4})/i',
	'/<\?php(\d+)\?\>/e'),  // Escaped the \>, but not necessary, same reason
array(
	'_pcre_emails(\'$1\',$emails,$email_counter)',
	'<a href="mailto:$1">$1</a>',
	'$emails[$1]'),
$text)),"\n";
?>

OUTPUT:
I am using this code to convert a [email protected] to <a href="mailto:[email protected]">[email protected]</a>
I am using this code to convert a <a href="mailto:[email protected]">[email protected]</a> to <a href="mailto:[email protected]">[email protected]</a>

One solution is to remove the already-linked e-mail addresses, then replace what's left.  I do this by replacing them with a special piece of bookmark code containing the index of the array where I've saved them, replace the remaining e-mails, and then return the "stored" strings to their places.

 

<?php

function _pcre_emails ($txt,&$array_ref,&$counter_ref) { $array_ref[$counter_ref] = str_replace('\"','"',$txt); return '<?php' . $counter_ref++ . '?' . '>'; }
// (Separated ? and > above so PHP highlighting would work, but not necessary in code)
$emails = array();
$email_counter = 0;

$text = 'I am using this code to convert a [email protected] to <a href="mailto:[email protected]">[email protected]</a>';
echo "\n",htmlentities($text),"\n";
echo htmlentities(preg_replace(
array(
	'/(<a href="mailto:.*?">.*?<\/a>)/ie',
	'/([\w.-]+@(?:[\w-]+\.){1,3}\w{2,4})/i',
	'/<\?php(\d+)\?\>/e'),  // Escaped the \>, but not necessary, same reason
array(
	'_pcre_emails(\'$1\',$emails,$email_counter)',
	'<a href="mailto:$1">$1</a>',
	'$emails[$1]'),
$text)),"\n";
?>

OUTPUT:
I am using this code to convert a [email protected] to <a href="mailto:[email protected]">[email protected]</a>
I am using this code to convert a <a href="mailto:[email protected]">[email protected]</a> to <a href="mailto:[email protected]">[email protected]</a>

 

 

 

That's way way too long, I figured it out with some help on another forum, try this, it's much cleaner....

 

$text = preg_replace("~(?>[\w.-]+@[\w.-]+)(?!</a>)(?!\">)~","<a href=\"mailto:\\0\">\\0</a>", $text);

 

Thanks anyway.

 

Ah, the once-only subpattern operator... that's what I needed.  I started replying with a simple lookbehind regex, but while it would ignore [email protected] it would then find [email protected] since "s" wasn't preceeded by the "mailto:" bit.

 

So, thanks, I learned about the once-only operator.

 

Also,

$text = preg_replace('~(?<!href="mailto:|>)(?>[\w.-]+@(?:[\w-]+\.){1,3}[a-z]{2,6})(?!">|</a>)~i','<a href="mailto:$0">$0</a>', $text);

($0 preferred to \\0; neg. lookaheads can be collapsed; double to single quotes)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.