Page 1 of 1

Parsing an email

Posted: Sun Jan 15, 2006 7:10 pm
by gar
Hi everybody,
I am grabbing emails from a pop3 mailbox and the body is in this format:

Code: Select all

subject:  blah blah
Name: Andrew 
Company: whatever
MSISDN: 12323232321
Email: acsXcz@whatever.com
Camera: yes 
UseSMS: yes 
SMSsent: yes 
UseMMS: yes 
MMSsent: yes 
Ringtone: yes 
Operator: Vodafone 
Favourite: Dance 
Question: when is th event?
Interactive: Yes 
Timestamp: 13/01/2006 13:30
ive never parsed strings before with php. how would i go about plucking out the MSISDN and the operator?

Posted: Sun Jan 15, 2006 7:41 pm
by Jenk

Code: Select all

<?php

$lines = explode('\n', $email);
$data = array();

foreach ($lines as $line) {
    list($key, $val) = explode(': ', $line);
    $data[trim($key)] = trim($val);
}

?>
Where $email contains the contents of the mail as a string.

Posted: Sun Jan 15, 2006 7:48 pm
by gar
awesome! thank you

Posted: Sun Jan 15, 2006 7:50 pm
by Jenk
I had a stray 0 in the line "$data = array();" I have updated :)

Posted: Tue Jan 17, 2006 3:00 am
by gar
after messing with this for literally HOURS i realised you need double quotes around the \n

Posted: Tue Jan 17, 2006 3:35 am
by Jenk
doh!

My bad, sorry. :oops: