PHP Loop through String (Email Body) and extract data

I am retrieving email using the PHP function imap_open() from a pop3 server. What I need to do is extract the body of the emails (ALL THE EMAILS HAVE THE SAME STRUCTURE), and enter the data that comes after the colon ':'. This is an example of a email-->

name: Thomas J Davis

email: detexoo7@yahoo.com

Telephone: 972-215-9526

vehicle: Van fullsize

Vehicle_Make: gmc

Vehicle_Model: 2500

Vehicle_Year: 1995

from_city: san francisco

from_state: California

to_city: grand prairie

to_state: Texas

ship_month: March

ship_day: 11

ship_year: 2010

shipping-information: no shipping info listed


I need to figure out how to loop through the BODY (above example), and analyze each line to get the data after the colon to insert into a mysql database. 500 points.
 
LVL 11
Andrew AngellCo-Owner / DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nasirbestCommented:
do it in this way
$lines = array();
$data = array();
$key = '';
$val = '';

$lines = explode($body, '\n')l
foreach ($lines as $line) {
    list($key, $val) = explode($line, ': ');
    mysql_query("INSERT INTO tbl_table (key, value) values ('$key', '$value')");
    $data[][$key] = $val;
}

print_r($data);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
svenarildCommented:
I assume that you have already fetched the body of the email by using imap_body().

In the code example, the $body variable would contain the content of the email body.

The piece of code is entering the different entries inside the email body into an array; if the line in question contain a colon, if it dont it is skipped.

To access said values that was in the body when entering them into the database, or if you need to modify the value, you just need to use the $data array.

Example (using you example data):
$data['from_state'] would contain California
$data['telephone'] would contain 972-215-9526

You can also easily check the content by doing a var_dump or print_r on the array.
$content = explode("\n\r", $body);

$data = array();

foreach ($content as $temp) {
	if (strpos($temp, ':') !== false) {
		list($key, $value) = explode(':', $temp);

		$data[trim(strtolower($key))] = trim($value);
		}
	}

Open in new window

0
Andrew AngellCo-Owner / DeveloperAuthor Commented:
I tried both solutions as is, they are off. I am going to try to alter the code to get it to work. If you can test it and get it working please post it so i can dish out these 500 points. Much appreciated.
0
svenarildCommented:
The solution works with the data you provided. Most probably the problem is that the data does not look like you said it would.

Can you post your current code and an copy of one of the email as attached files?
0
Andrew AngellCo-Owner / DeveloperAuthor Commented:
svenarild, i've taken the two examples above, and I have altered them. Mostly the problem is the html that some emails send to format the email message. What i've done is use nls2br() to convert the next line & return characters to <br>. Then i've exploded on "<br>". Thanks for all the help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.