Jump to content

Extract emails from addresses in To fields


mapleleaf

Recommended Posts

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,

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.