views:

62

answers:

3

I receive leads via email when someone submits a form residing on a website on which I pay to advertise. Using PHP, I can access my email account (IMAP), get and read messages, but I'm having trouble extracting certain information from the body of the email. I can explode (\n) the body but cannot figure out how to extract the needed information to populate variables in my MYSQL Insert function.

After using explode() displaying the results looks like this:

Subjects :: ADVERTISINGDOMAIN.COM - RSVP Open House Inquiry - Listing #839027
To :: [email protected]
ToName :: Matthew
From :: [email protected]
FromName :: XYZ Leads

RSVP for Open House

Listing #: 8390279
Open House Date: Sep 3, 2010
Name: John Doe
Phone: 5555555555
Email: [email protected]
Comments: Hi. I will be at the open house...looking forward to meeting you and viewing this property. Thanks. Jane

Warm Regards,
Matthew

A: 

if you use preg_split you can split your data via :+ and separate each part out

$str = <<<END
Subjects :: ADVERTISINGDOMAIN.COM - RSVP Open House Inquiry - Listing #839027
To :: [email protected]
ToName :: Matthew
From :: [email protected]
FromName :: XYZ Leads

RSVP for Open House

Listing #: 8390279
Open House Date: Sep 3, 2010
Name: John Doe
Phone: 5555555555
Email: [email protected]
Comments: Hi. I will be at the open house...looking forward to meeting you and viewing this property. Thanks. Jane
END;

$parts = explode( "\n", $str );

foreach( $parts as $part ) {
    // split by :+ and unlimited spaces
    $data = preg_split( '~[:]+\s*~', trim($part), null, PREG_SPLIT_NO_EMPTY );
    if ( count( $data ) ) {
        $final_data[] = $data;
    }

}
print_r($final_data);

results in...

Array
(
    [0] => Array
        (
            [0] => Subjects 
            [1] => ADVERTISINGDOMAIN.COM - RSVP Open House Inquiry - Listing #839027
        )

    [1] => Array
        (
            [0] => To 
            [1] => [email protected]
        )

    [2] => Array
        (
            [0] => ToName 
            [1] => Matthew
        )
...

Now if you loop through that array you have the "variable name" as position 0 of each array and the variable data at position 1. If they dont directly line up with table fields you could create an array that maps form fields to table fields.

Galen
Thanks. Using preg_split I was able to get the following. Could this work? How do I loop through to assign each to a variable? Here's the output I received.Array ( [0] => ADVERTISINGDOMAIN.COM - RSVP Open House Inquiry - Listing #839027Listing # [1] => 839027Open House Date [2] => Sep 3, 2010Name [3] => John DoeEmail [4] => [email protected] [5] => 555555555Comments [6] => Hi. I will be at the open house...looking forward to meeting you and viewing this property. Thanks. Jane
Matthew
+1  A: 

Alright, use explode() once more on every string with limit and you will get the fields like this:

$var = explode(':', 'Name: John Doe', 2);

This will give you $var[0] containing the field name as Name and $var[1] containing the value as John Doe. Now do this for every line and then use the values in your query.

shamittomar
Not sure I follow. My initial explode is:$body_lines = explode("\n", $body); unset($dataTxt); foreach ($body_lines as $x => $line) { $body_lines .= htmlspecialchars($line) . "<br />\n";So, now I need to follow with this?$body_lines = explode(':', $body_lines, 2);
Matthew
A: 

which information are you specifically looking for?

Drewdin
this is more comment material
Galen
Hey Galen - I'm specifically wanting to get the data contained in Listing #, Date, Name, Phone, Email, and Comments.
Matthew