无法通过电子邮件和 php 中的 URL 链接文本

Unable to linkfy with text with both email and URL in php

我正在尝试编写将给定文本中的 linkfy(转换为超链接)电子邮件和 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://" target="_blank" title="[=12=]">[=12=]</a>', $noteText);
        }


        return $noteText;
}


$str = "contact me at test.me@gmail.com visit us http://google.com ,http://gmail.com";
function ($str);

这段代码怎么样

 1  #!/usr/bin/php
 2  <?php
 3  function linkifyMyEmail($noteText) 
 4  {
 5          $emailPattern = '/(\S+@\S+\.\S+)/';
 6          if(preg_match($emailPattern, $noteText, $email)) {
 7              // change email to mailto 
 8              $replace = '<a href="mailto:' . $email[0] . '">' . $email[0] . '</a>';
 9              $noteText = preg_replace($emailPattern, $replace, $noteText);
10          }
11          return $noteText;
12  }
13  function linkifyMyUrl($noteText)      
14  {
15          $urlPattern = '@(http://\S+)@';
16          if(preg_match($urlPattern, $noteText, $url)) {
17          // change URLs to hyperlinks
18              $replacement = '<a href="" target="_blank" title=""></a>';
19              $noteText = preg_replace($urlPattern,$replacement,$noteText);
20          }
21          return $noteText;
22  }
23  
24  
25  $str = "contact me at test.me@gmail.com visit us http://google.com ,http://gmail.com";
26  printf("Original string is %s\n",$str);
27  printf("string after email convertion %s\n",linkifyMyEmail($str));
28  printf("string after url convertion %s\n",linkifyMyUrl($str));
29  printf("Combining both functions %s\n",linkifyMyUrl(linkifyMyEmail($str)));
30  ?>

并输出

[root@vznfsclient ~]# ./Test.php 
Original string is contact me at test.me@gmail.com visit us http://google.com ,http://gmail.com
string after email convertion contact me at <a href="mailto:test.me@gmail.com">test.me@gmail.com</a> visit us http://google.com ,http://gmail.com
string after url convertion contact me at test.me@gmail.com visit us <a href="http://google.com" target="_blank" title="http://google.com">http://google.com</a> ,<a href="http://gmail.com" target="_blank" title="http://gmail.com">http://gmail.com</a>
Combining both functions contact me at <a href="mailto:test.me@gmail.com">test.me@gmail.com</a> visit us <a href="http://google.com" target="_blank" title="http://google.com">http://google.com</a> ,<a href="http://gmail.com" target="_blank" title="http://gmail.com">http://gmail.com</a>

我认为在基于交替的正则表达式中使用您的模式并在 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);

this PHP demo