AviNahum Posted February 14, 2012 Share Posted February 14, 2012 hey, im trying to recognize an email address in a text paragraph, for example: 1111111 <br/> 123emailaddress123@gmail.com <br/> 22222 and replace it to: 1111111 <br/> <a href="mailto:123emailaddress123@gmail.com">123emailaddress123@gmail.com</a> <br/> 22222 it's my first time dealing with regex and so far i reached this: $text = "1111111 <br/> 123emailaddress123@gmail.com <br/> 22222"; $post_eregi = '/(.)([^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4})(.)/'; echo preg_replace($post_eregi, "<a href='mailto:$2'>$2</a>", $text); unfortunately it doesnt work but if i remove the breakline tag before the email address it's works grate! any suggestions or ideas what wrong? thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/257092-finding-an-email-in-a-text/ Share on other sites More sharing options...
AyKay47 Posted February 14, 2012 Share Posted February 14, 2012 hello, you can use something like this: $pattern = "~\b([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b~"; $str = "1111111 <br/> 123emailaddress123@gmail.com <br/> 22222"; $replacement = '<a href="mailto:$1">$1</a>'; echo preg_replace($pattern, $replacement,$str); results: 1111111 <br/> <a href='mailto:123emailaddress123@gmail.com'>123emailaddress123@gmail.com</a> <br/> 22222 Quote Link to comment https://forums.phpfreaks.com/topic/257092-finding-an-email-in-a-text/#findComment-1317946 Share on other sites More sharing options...
AviNahum Posted February 14, 2012 Author Share Posted February 14, 2012 thanks for your replay... it's not working at all :| Quote Link to comment https://forums.phpfreaks.com/topic/257092-finding-an-email-in-a-text/#findComment-1317952 Share on other sites More sharing options...
AyKay47 Posted February 14, 2012 Share Posted February 14, 2012 append an i modifier to the end of the pattern to make it case insensitive, I was hoping you would take some initiative and see that. $pattern = "~\b([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})\b~i"; $str = "1111111 <br/> 123emailaddress123@gmail.com <br/> 22222"; $replacement = '<a href="mailto:$1">$1</a>'; echo preg_replace($pattern, $replacement,$str); Quote Link to comment https://forums.phpfreaks.com/topic/257092-finding-an-email-in-a-text/#findComment-1317962 Share on other sites More sharing options...
AviNahum Posted February 14, 2012 Author Share Posted February 14, 2012 as i said, it's my first time dealing with regex... it's works great! thank alot! Quote Link to comment https://forums.phpfreaks.com/topic/257092-finding-an-email-in-a-text/#findComment-1317963 Share on other sites More sharing options...
AyKay47 Posted February 14, 2012 Share Posted February 14, 2012 no problem, please mark this thread as solved (bottom left corner button). Quote Link to comment https://forums.phpfreaks.com/topic/257092-finding-an-email-in-a-text/#findComment-1317965 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.