mapleleaf Posted April 23, 2012 Share Posted April 23, 2012 If I have a string like this: Anne Somebody <[email protected]>, John Smith <[email protected]>, bob Someoneelse <[email protected]> is there a simple way of extracting the emails? I can do the explode and trim so I am really asking how to handle the John Smith <[email protected]> in a reliable way. To be left with [email protected] Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/261500-extract-emails-from-addresses-in-to-fields/ Share on other sites More sharing options...
xyph Posted April 23, 2012 Share Posted April 23, 2012 Regular Expressions. <?php $str = 'Anne Somebody <[email protected]>, John Smith <[email protected]>, bob Someoneelse <[email protected]>'; preg_match_all( '/<([^>]++)>/', $str, $matches ); print_r( $matches ); ?> Returns Array ( [0] => Array ( [0] => <[email protected]> [1] => <[email protected]> [2] => <[email protected]> ) [1] => Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] ) ) The RegEx finds a <, then matches any character that isn't a >, then matches a >. The brackets capture sub-results. Quote Link to comment https://forums.phpfreaks.com/topic/261500-extract-emails-from-addresses-in-to-fields/#findComment-1339930 Share on other sites More sharing options...
mapleleaf Posted April 23, 2012 Author Share Posted April 23, 2012 Perfect. Exactly what I needed. Thanks a lot!! Quote Link to comment https://forums.phpfreaks.com/topic/261500-extract-emails-from-addresses-in-to-fields/#findComment-1339938 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.