我正在尝试编写将在给定文本中链接(转换为超链接)电子邮件和URL的功能,但在替换电子邮件时面临问题,因为它将包含域。 有些人可以更正我的代码,它应该替换电子邮件中的域名吗?
代码示例
function linkifyMyString($noteText)) { $emailPattern = '/(\S+@\S+\.\S+)/'; $urlPattern = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@'; if(preg_match($emailPattern, $noteText, $email)) { // change email to mailto $replace = "<a href='mailto:'.$email[0].'>".$email[0]."</a>"; $noteText = preg_replace($emailPattern, $replace, $noteText); } if(preg_match($urlPattern, $noteText, $url)) { // change URLs to hyperlinks $noteText = preg_replace($urlPattern, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $noteText); } return $noteText; } $str = "contact me at test.me@gmail.com visit us http://google.com ,http://gmail.com"; function ($str);I am trying to write function which will linkfy (convert as hyperlinks) email and URLs in the given text, but facing issue while replacing email since it will have domain in it. can some please correct my code where it should replace domain name in email?
Code Sample
function linkifyMyString($noteText)) { $emailPattern = '/(\S+@\S+\.\S+)/'; $urlPattern = '@(http)?(s)?(://)?(([a-zA-Z])([-\w]+\.)+([^\s\.]+[^\s]*)+[^,.\s])@'; if(preg_match($emailPattern, $noteText, $email)) { // change email to mailto $replace = "<a href='mailto:'.$email[0].'>".$email[0]."</a>"; $noteText = preg_replace($emailPattern, $replace, $noteText); } if(preg_match($urlPattern, $noteText, $url)) { // change URLs to hyperlinks $noteText = preg_replace($urlPattern, '<a href="http$2://$4" target="_blank" title="$0">$0</a>', $noteText); } return $noteText; } $str = "contact me at test.me@gmail.com visit us http://google.com ,http://gmail.com"; function ($str);最满意答案
我认为在基于交替的正则表达式中使用模式并在preg_replace_callback替换更优雅和高效,因为您只使用1个正则表达式传递并且可以在需要时轻松地自定义替换逻辑:
function replace_callback($m){ if (empty($m[3])) { // email return "<a href='mailto:".$m[0]."'>" . $m[0] . "</a>"; } else { // url return "<a href='".$m[1]."://".$m[3]."' target='_blank' title='".$m[0]."'>".$m[0]."</a>"; } } function linkifyMyString($noteText) { $emailPattern = '\S+@\S+\.\S+'; $urlPattern = '(https?)?(://)?([a-zA-Z](?:[-\w]+\.)+(?:[^\s.]+\S*)+[^,.\s])'; return preg_replace_callback('~' . $emailPattern . '|' . $urlPattern . '~', 'replace_callback', $noteText); } $str = "www.google.com contact me at test.me@gmail.com visit us http://google.com ,http://gmail.com"; echo linkifyMyString($str);看这个PHP演示
I think using your patterns in an alternation based regex and replacing inside a preg_replace_callback is more elegant and efficient, as you only use 1 regex pass and can customize your replacement logic easily when needed:
function replace_callback($m){ if (empty($m[3])) { // email return "<a href='mailto:".$m[0]."'>" . $m[0] . "</a>"; } else { // url return "<a href='".$m[1]."://".$m[3]."' target='_blank' title='".$m[0]."'>".$m[0]."</a>"; } } function linkifyMyString($noteText) { $emailPattern = '\S+@\S+\.\S+'; $urlPattern = '(https?)?(://)?([a-zA-Z](?:[-\w]+\.)+(?:[^\s.]+\S*)+[^,.\s])'; return preg_replace_callback('~' . $emailPattern . '|' . $urlPattern . '~', 'replace_callback', $noteText); } $str = "www.google.com contact me at test.me@gmail.com visit us http://google.com ,http://gmail.com"; echo linkifyMyString($str);See this PHP demo
更多推荐
$noteText,email,function,$urlPattern,$emailPattern,电脑培训,计算机培训,IT培训"/>
发布评论