simonp Posted September 18, 2009 Share Posted September 18, 2009 Hi, I have a string: \"joe bloggs\" <joe@joebloggs.co.uk> How can I extract the e-mail address (inside the < >) into another string? Thanks Simon Quote Link to comment https://forums.phpfreaks.com/topic/174671-solved-extract-e-mail-address-from-string/ Share on other sites More sharing options...
Bricktop Posted September 18, 2009 Share Posted September 18, 2009 Hi simonp, Something like the below should do the trick: $string = "\"joe bloggs\" <joe@joebloggs.co.uk>"; ereg('.* (.*@[^ ]* )',$string,$m); echo $m[1] . "\n" Quote Link to comment https://forums.phpfreaks.com/topic/174671-solved-extract-e-mail-address-from-string/#findComment-920535 Share on other sites More sharing options...
simonp Posted September 18, 2009 Author Share Posted September 18, 2009 Hi, Will that work no matter how many dots in the e-mail address? ie joe.bloggs@joebloggs.co.uk or joebloggs@joebloggs.com Quote Link to comment https://forums.phpfreaks.com/topic/174671-solved-extract-e-mail-address-from-string/#findComment-920536 Share on other sites More sharing options...
Bricktop Posted September 18, 2009 Share Posted September 18, 2009 Hi simonp, It should do, or you could try: $string = "\"joe bloggs\" <joe@joebloggs.co.uk>"; ereg("^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$", $string, $m); echo $m[1]; Quote Link to comment https://forums.phpfreaks.com/topic/174671-solved-extract-e-mail-address-from-string/#findComment-920540 Share on other sites More sharing options...
simonp Posted September 18, 2009 Author Share Posted September 18, 2009 Thanks for your help Bricktop - unfortunately, it doesn't work Would it not be easier/safer to remove everything before the < and after the >? Is that possible? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/174671-solved-extract-e-mail-address-from-string/#findComment-920547 Share on other sites More sharing options...
Bricktop Posted September 18, 2009 Share Posted September 18, 2009 Good point, how about: $string = "\"joe bloggs\" <joe@joebloggs.co.uk>"; $result = split('[<>]', $string); echo $result[1]; Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/174671-solved-extract-e-mail-address-from-string/#findComment-920561 Share on other sites More sharing options...
simonp Posted September 18, 2009 Author Share Posted September 18, 2009 Bricktop - you're a genius! Great little bit of code - thanks! Quote Link to comment https://forums.phpfreaks.com/topic/174671-solved-extract-e-mail-address-from-string/#findComment-920565 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.