I'm currently matching email addresses with the below regex (and linking them as mailto
links):
/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,64})/
However, I do not want to link anything that has already been linked before as a mailto
link or within an existing link (eg. index.php?email=example@example.com) otherwise the link gets quite screwed up like the below:
<a href="mailto:<a href="mailto:example@example.com">example@example.com</a>"><a href="mailto:example@example.com">example@example.com</a></a>
Update
Here's an example of PHP I'm using to find the email addresses (sorry, didn't think it was required):
$input = "example@example.com<br><br><a href='mailto:example@example.com'>test email</a><br><br><a href='mailto:example@example.com'>example@example.com</a>";
$output = preg_replace("/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,64})/i", "<a href='mailto:$1'>$1</a>", $input);
//output
<a href="mailto:example@example.com">example@example.com</a><br><br><a mailto:example@example.com'="" href="mailto:<a href=">example@example.com</a>'>test email<br><br><a mailto:example@example.com'="" href="mailto:<a href=">example@example.com</a>'><a href="mailto:example@example.com">example@example.com</a>
Update 2
I also have a further regex question (if possible) - I also have the below regex to make all links target to a new window, however, i do not want anything mailto
linked to go to a new window - is it possible to not target mailto
links?
$output = preg_replace("/<(a)([^>]+)>/i", "<\\1 target=\"_blank\"\\2>", str_replace('target="_blank"', '', $output));