[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

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.
Andrew Angell
Andrew Angell
  • 2
  • 2
2 Solutions
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;


Open in new window

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

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.
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?
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.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now