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);