jaymc Posted January 22, 2009 Share Posted January 22, 2009 Im looking for some code to extract an email address from a string, It needs to work for the following $string = "email@site.com" $string = "hello my email email@site.com" $string = "hello my email email@site.com add it please" In all 3 cases it must return email@site.com with no trailing spaces or any other of the surrounding text Quote Link to comment https://forums.phpfreaks.com/topic/142046-solved-extract-email-address-from-string/ Share on other sites More sharing options...
gevans Posted January 22, 2009 Share Posted January 22, 2009 I got this straight of a tutorials website as it's a lot faster than writting it... <?php function extract_emails_from($string){ preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches); return $matches[0]; } $text = "blah blah blah email@address.com blah blah blah email2@address.com"; $emails = extract_emails_from($text); print(implode("\n", $emails)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/142046-solved-extract-email-address-from-string/#findComment-743824 Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 Beatin to it, but hey I did alot of hard work...actually I just pulled this from preg_match and modified a bit <?php $string = "hello my email email@site.com"; $pattern = '/([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])' . '(([a-z0-9-])*([a-z0-9]))+' . '(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)/i'; preg_match ($pattern, $string, $matches); echo "We extracted " . $matches[0] . " from $string"; ?> Should do it. Quote Link to comment https://forums.phpfreaks.com/topic/142046-solved-extract-email-address-from-string/#findComment-743827 Share on other sites More sharing options...
jaymc Posted January 23, 2009 Author Share Posted January 23, 2009 Excellent Quote Link to comment https://forums.phpfreaks.com/topic/142046-solved-extract-email-address-from-string/#findComment-743837 Share on other sites More sharing options...
jaymc Posted January 23, 2009 Author Share Posted January 23, 2009 Ok.. How about this scenario, slightly trickier $string = "hello go here www.website.com/jaymc please" $string = "hello go here http://www.website.com/jaymc please" $string = "hello go here website.com/jaymc please" $string = "hello go here http://website.com/jaymc please" In all 3 cases it must return the URL in any of its forms (www.website.com/jaymc, http://www.website.com/jaymc, website.com/jaymc, http://website.com/jaymc) with no trailing spaces or any other of the surrounding text Quote Link to comment https://forums.phpfreaks.com/topic/142046-solved-extract-email-address-from-string/#findComment-743861 Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 Are you testing us, or are you just not trying to find a solution on your own? EDIT.... I need to be a little more constructive... Check out Nick's Regular Expressions Tutorial I just did and it's a great read, will give you an insight into what you're trying to do and more... Quote Link to comment https://forums.phpfreaks.com/topic/142046-solved-extract-email-address-from-string/#findComment-743887 Share on other sites More sharing options...
redarrow Posted January 23, 2009 Share Posted January 23, 2009 arr i get it, you didn't understand preg_match. http://www.regexbuddy.com/ try this link Quote Link to comment https://forums.phpfreaks.com/topic/142046-solved-extract-email-address-from-string/#findComment-743892 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.