9 Replies - 770 Views - Last Post: 23 August 2015 - 11:44 AM Rate Topic: -----

#1 mutago234   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 260
  • Joined: 08-September 13

Explode data in php

Posted 23 August 2015 - 03:07 AM

Hello everyone, am trying to get messages on my email.

Lets assume in my Gmail Inbox I have just one message subject from a friend titled "How are you mutago"

In the body of the message I have " Hello Mutago how are you doing. I wish you are fine".

Now I respond to my friends message as per "Well My friend am doing fine."


As you can see now under the Message subject "How are You mutago"

We have 2 messages or responses ie one from me and other from my friend who sent the mail.

my question is how do i get the two seperate messages/rows and insert into database.

The code below only explode and insert data line by line. In that case I have more than 10 rows inserted into my database
instead of just 2 rows corresponding to the two messages on my mail box.

I guess the problem is from explode function, please how do i fix that.

$str = $email_body_messages;
$piece = explode(PHP_EOL, $str);
foreach($piece as $substr){

// Insert into database via PDO

    }



Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Explode data in php

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Explode data in php

Posted 23 August 2015 - 03:31 AM

So $email_body_messages contains the full text of the message(s), including newlines?

You obviously cannot split at newlines as you've discovered, so what distinguishes one message-text from the other? Is there a blank line between them? Is there consistently a blank line between them? Bear in mind that if someone is directly adding their message above (or below?) the previous message then it is possible that they might remove the blank line between them, or add more.

This post has been edited by andrewsw: 23 August 2015 - 03:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 mutago234   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 260
  • Joined: 08-September 13

Re: Explode data in php

Posted 23 August 2015 - 03:58 AM

Below is how the message looks like in the mail box. it seems to be sperated by long series of doted lines and date/time for which the message wa sent

##- Please type your reply above this line -##

Re: Hello Mutago how are you doing

----------------------------------------------

Dan Corbin, Aug 22, 11:10

Hi Vijah,

Okay take Care
Thanks!

Dan

---
Dan mutago, Product Manager

----------------------------------------------

Mutago, Aug 22, 09:54

Hi friend -

Am okay what about you
Cheers,

Dan mutago


----------------------------------------------

Vijah, Aug 22, 01:13

Hello Mutago how are you doing. i wished you are fine

Thanks

--------------------------------







Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Explode data in php

Posted 23 August 2015 - 04:06 AM

So you could split this long string at newlines, then iterate the resulting array creating individual messages, looking for a sequence of dashes to signify the end of a message.
Was This Post Helpful? 0
  • +
  • -

#5 mutago234   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 260
  • Joined: 08-September 13

Re: Explode data in php

Posted 23 August 2015 - 04:21 AM

Please can you illustrate an example on how to accomplish that please
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Explode data in php

Posted 23 August 2015 - 04:43 AM

You already have code to iterate each line.
You need another array.
While iterating each line, you need some if statements to check the line you are reading.
Use a temporary string to collect the lines for the current message.
Either:
Add the current line to the existing message;
Discard the line;
Finish the existing message, adding it to the array, and start a new message.

You need to make an attempt at this.
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Explode data in php

Posted 23 August 2015 - 07:16 AM

I'd explode on the long line of dashes, then start at $piece[1] since $piece[0] will hold the sender. Since the long list seems to vary in length, I'd explode on something like "---------\n". You might want to filter out the remaining dashes, but you should have all the data separated.
Was This Post Helpful? 0
  • +
  • -

#8 mutago234   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 260
  • Joined: 08-September 13

Re: Explode data in php

Posted 23 August 2015 - 10:24 AM

my poor brain is burning, can you provide me with an example on this @CTphpnwb
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Explode data in php

Posted 23 August 2015 - 11:13 AM

See what you get with this:
$email = <<<here
##- Please type your reply above this line -##

Re: Hello Mutago how are you doing

----------------------------------------------

Dan Corbin, Aug 22, 11:10

Hi Vijah,

Okay take Care
Thanks!

Dan

---
Dan mutago, Product Manager

----------------------------------------------

Mutago, Aug 22, 09:54

Hi friend -

Am okay what about you
Cheers,

Dan mutago


----------------------------------------------

Vijah, Aug 22, 01:13

Hello Mutago how are you doing. i wished you are fine

Thanks

--------------------------------





here;

$test = explode("----\n",$email);
print_r($test,1);


Was This Post Helpful? 0
  • +
  • -

#10 mutago234   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 260
  • Joined: 08-September 13

Re: Explode data in php

Posted 23 August 2015 - 11:44 AM

Tank You
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1