我有一段代码对于用实际链接替换电子邮件地址非常有用。有时电子邮件地址有多个后缀(即:.co.uk和.com)。我能够为这些场景中的每一个创建正则表达式,但是我很好奇,如果有任何正则表达式的大师知道将两者结合成单个表达式的方法。如果是这样,你能解释一下你的答案是什么以及它为什么起作用?对于多种可能性的正则表达式
这里是我当前的代码 -
$input = "here is a line of text, [email protected], [email protected], [email protected] here";
preg_match_all('%\w+\@\w+\.\w+\.\w+%', $input, $matches);
$outmatch = Array();
if(is_array($matches[0])){
foreach($matches[0] as $match){
array_push($outmatch,$match);
}
}
$outmatch = array_unique($outmatch);
if(is_array($outmatch)){
foreach($outmatch as $outm){
$input = str_replace($outm,'<a href="mailto:' . $outm . '">' . $outm . '</a>',$input);
}
}
print $input;
表达2后缀:%\w+\@\w+\.\w+\.\w+%
表达式1个后缀:%\w+\@\w+\.\w+%
你为什么讨厌我的'有人+东西@ example.com' EMAILADDRESS? – Wrikken 2013-03-04 21:50:49
因为这不自然!好的一点,谢谢。 – user1145643 2013-03-04 21:59:22