somu.web
somu.web

Reputation: 387

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

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 [email protected] visit us http://google.com ,http://gmail.com";
function ($str);

Upvotes: 1

Views: 86

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627087

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 [email protected] visit us http://google.com ,http://gmail.com";
echo linkifyMyString($str);

See this PHP demo

Upvotes: 1

Dmitry Zayats
Dmitry Zayats

Reputation: 473

How about this code

 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="$1" target="_blank" title="$1">$1</a>';
19              $noteText = preg_replace($urlPattern,$replacement,$noteText);
20          }
21          return $noteText;
22  }
23  
24  
25  $str = "contact me at [email protected] 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  ?>

And output

[root@vznfsclient ~]# ./Test.php 
Original string is contact me at [email protected] visit us http://google.com ,http://gmail.com
string after email convertion contact me at <a href="mailto:[email protected]">[email protected]</a> visit us http://google.com ,http://gmail.com
string after url convertion contact me at [email protected] 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:[email protected]">[email protected]</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>

Upvotes: 1

Related Questions