我目前正在使用以下正则表达式匹配电子邮件地址,并将它们作为 mailto
链接连接:
/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,64})/
然而,我不想将已经作为mailto
链接或在现有链接中(例如index.php?email=example@example.com)链接的任何内容再次链接,否则链接会变得非常混乱,如下所示:
<a href="mailto:<a href="mailto:example@example.com">example@example.com</a>"><a href="mailto:example@example.com">example@example.com</a></a>
更新
以下是我正在使用的PHP示例,用于查找电子邮件地址(抱歉,我认为这是必需的):
$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>
更新 2
我还有一个正则表达式的问题(如果可能) - 我也有以下正则表达式,使所有链接都指向一个新窗口,然而,我不想让任何链接到 mailto
的东西去新窗口 - 是否有可能不指向 mailto
链接?
$output = preg_replace("/<(a)([^>]+)>/i", "<\\1 target=\"_blank\"\\2>", str_replace('target="_blank"', '', $output));