Re: regular expression
| From: | Curt Zirzow | Date: | Wed, 07 Apr 2004 19:11:49 +0000 |
| Subject: | Re: regular expression | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-182691@lists.php.net to get a copy of this message | ||
* Thus wrote Robert Kornfeld (aon.912628966@aon.at):
> 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: foo@foo.com' or
> 'To: first foo <foo@foo.com>'
Actually the second one needs to be
"first foo" <foo@foo.com> - or -
foo <foo@foo.com>
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@ddress.com; "foo bar" <another@t.com>
$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."