mapleleaf Posted April 23, 2012 Share Posted April 23, 2012 If I have a string like this: Anne Somebody <anne@email.com>, John Smith <john@hotmail.co.uk>, bob Someoneelse <bob@email.it> 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 <john@hotmail.co.uk> in a reliable way. To be left with john@hotmail.co.uk Thanks, Quote Link to comment Share on other sites More sharing options...
xyph Posted April 23, 2012 Share Posted April 23, 2012 Regular Expressions. <?php $str = 'Anne Somebody <anne@email.com>, John Smith <john@hotmail.co.uk>, bob Someoneelse <bob@email.it>'; preg_match_all( '/<([^>]++)>/', $str, $matches ); print_r( $matches ); ?> Returns Array ( [0] => Array ( [0] => <anne@email.com> [1] => <john@hotmail.co.uk> [2] => <bob@email.it> ) [1] => Array ( [0] => anne@email.com [1] => john@hotmail.co.uk [2] => bob@email.it ) ) The RegEx finds a <, then matches any character that isn't a >, then matches a >. The brackets capture sub-results. Quote Link to comment 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 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.