Jetoox
Jetoox

Reputation: 1437

imap headers returned formatted string broken down into values

I have a string that is returned via imap_headers() which has a format like this one:

Flags Number) Date Sender Subject (Size)

and sample returned string from an array is:

A 454)22-Jun-2004 [email protected] Your Amazon.co.uk Order (5284 chars)
1)10-Oct-2010 Gmail Team Get Gmail on your mobile (2457 chars)
5)12-Nov-2010 =?ISO-8859-1?Q?Eric_ For Website (671669 chars)
U 6)17-Nov-2010 no-reply@hostmessage host.net account : boom (2082 chars)

I want to decode/parse this string and create an object out from this string. e.g

Flags
Date
Sender
Subject

i find it hard parsing this using php string functions because, this string is just separated by a space. Can anybody help me on this? The sender part could also be a name, so we can just treat it as an email add or a website url.

Upvotes: 1

Views: 193

Answers (1)

Till Helge
Till Helge

Reputation: 9311

As I mentioned above in the comments, it's pretty much impossible to create a regular expression that will work for you, because you don't provide enough information. However, I will give you an example that will match your line and maybe you can go on from there:

preg_match('_([A-Z]+) (\d+)\)(\d+-[A-Za-z]+-\d{4}) (\S+@\S+\.\S+) (.+) \((\d+) chars\)_', $header, $matches);
var_dump($matches);

Now it depends on your application if you need to extend this expression to account for other variants of the possible output. You can read up on all the details of what I did there in the official documentation.

Upvotes: 2

Related Questions