simonp Posted September 18, 2009 Share Posted September 18, 2009 Hi, I have a string: \"joe bloggs\" <[email protected]> How can I extract the e-mail address (inside the < >) into another string? Thanks Simon 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\" <[email protected]>"; ereg('.* (.*@[^ ]* )',$string,$m); echo $m[1] . "\n" 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 [email protected] or [email protected] 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\" <[email protected]>"; 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]; 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 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\" <[email protected]>"; $result = split('[<>]', $string); echo $result[1]; Hope this helps. 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! 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
Archived
This topic is now archived and is closed to further replies.