* Thus wrote Robert Kornfeld ([EMAIL PROTECTED]):
> hey, professionals out there:
>
> i need to write a parser for an email-header to retrieve the email of the
> 'To:'-field.
> there are 2 possibilities:
> 'To: [EMAIL PROTECTED]' or
> 'To: first foo <[EMAIL PROTECTED]>'
Actually the second one needs to be
"first foo" <[EMAIL PROTECTED]> - or -
foo <[EMAIL PROTECTED]>
so you need something like:
/([^<]*<([^>]+)>|(.*))/
That will parse one address. if you have more than one address
you'll have to break the address list apart sperated by ';'
ie. To: [EMAIL PROTECTED]; "foo bar" <[EMAIL PROTECTED]>
$emails = explode(';', $email_list);
and to get that email address list you'll have to take into account
folding ie.
To: address;
address2@
domain.com
So to allow for this while reading the header lines:
$last_field = '';
foreach($headerline) {
// detect a folded header line
if ($headerline{1} == ' ' || $headerline{1} == "\t" ) {
$headers[$last_field] .= trim($headerline);
continue;
}
// check for end of headers
if ($headerline{1} == "\n") {
break;// we're done with headers.
}
//now split the field
list($field, $data) = explode(':', $headerline, 2);
// ensure name is consistent ok
$field = strtolower($field);
// save the data
$headers[$field] = $data;
// incase its folded
$last_field = $field;
}
Now apply the other two methods to $headers['to'];
that was a little more than a regex, but I hope it helps.
Curt
--
"I used to think I was indecisive, but now I'm not so sure."
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php