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, 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. 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!! 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
Archived
This topic is now archived and is closed to further replies.