Parsing an email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gar
Forum Newbie
Posts: 3
Joined: Sun Jan 15, 2006 7:00 pm

Parsing an email

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Last edited by Jenk on Sun Jan 15, 2006 7:49 pm, edited 1 time in total.
gar
Forum Newbie
Posts: 3
Joined: Sun Jan 15, 2006 7:00 pm

Post by gar »

awesome! thank you
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I had a stray 0 in the line "$data = array();" I have updated :)
gar
Forum Newbie
Posts: 3
Joined: Sun Jan 15, 2006 7:00 pm

Post by gar »

after messing with this for literally HOURS i realised you need double quotes around the \n
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

doh!

My bad, sorry. :oops:
Post Reply