Sometimes you need to extract an e-mail address from a format like this:
Name Surname <[email protected]>
In such cases use this function:
/** * Extract mail address from a string * * @param string $emailString * @return mixed match on success, false on error */ function extractEmail( $emailString ) { /** @var string $result **/ $result = preg_match( '/^(?:[\w.\- ]+<)?([\w._-]+@[\da-z\.-]+\.[a-z\.]{2,6})>?$/iu', $emailString, $matches ); if( $result === 1 || $result !== false ) { return $matches[1]; } else { return false; } }
Link to test regular expression used by me is here.