Re: Efficient way to filter this string
| From: | Steve Edberg | Date: | Thu, 22 Jul 2004 09:10:29 +0000 |
| Subject: | Re: Efficient way to filter this string | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-191492@lists.php.net to get a copy of this message | ||
At 2:18 AM -0600 7/22/04, C.F. Scheidecker Antunes wrote:
Hello, I have an email string that sometimes has a name and the email address between <> and sometimes has the email adress right away. Two examples of the possible value of $address Joe Doe <joe.doe@somedomain.com> jamie.doe@somedomain.com I need to convert this string always to the email address and discard the name to the left of the < character, if the string is already correct (like jamie.doe@somedomain.com) then do not change it, keep it as it is. I have writen a function to do a for loop and check for the < then copy everything to the end removing the >. I wonder, is there a more efficient, clear way to accomplish this? Thanks in advace.One way to do it (untested): $EmailAddress = preg_match('/<([^>]+)>/', $TestAddress, $MatchList) ? $MatchList[1] : $TestAddress ; $TestAddress would be the 'Joe Doe <joe.doe@somedomain.com>' or 'jamie.doe@somedomain.com' string, and $EmailAddress *should* be just the email address proper. If you want to validate the email address further, search the archives for email validation: http://marc.theaimsgroup.com/?l=php-general&r=1&w=2 See http://php.he.net/preg_match and ternary operators in http://us2.php.net/language.operators for more info. - steve -- +--------------- my people are the people of the dessert, ---------------+ | Steve Edberg sbedberg@ucdavis.edu | | University of California, Davis (530)754-9127 | | Programming/Database/SysAdmin http://pgfsun.ucdavis.edu/ | +---------------- said t e lawrence, picking up his fork ----------------+