Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

PHP - Parsing a 'plaintext' email body string to a MySQL DB

as far as searching this matter, I think I have done enough, or at least I've tried. I've found much of the answers here, for that thank to all, but, I still can't get the final touch, so, I decided to ask you guys. I'm trying to parse an email 'plaintext' part, that comes from Wix, they don't have a way of sending the forms to a DB, they only send them as emails. I've created a specific email address form this. I've searched and found PHP code in order to read that emai. With this I could see that the only part that matters to me is the 'plaintext' part of the email, see:

Clientes potenciais Nome: Jorge Gomes Endereço: 1234-123 Email: testes@email.com Telefone: 21874523745273 Dropdown Field: Limpeza ou Tratamento de Pavimentos Dropdown Field 2: Escritório Caixa de seleção: ✓ Verificado

With that in a string array, by using explode() and using ":" as the separator, I mananged to remove the extra text from each of the string's array in order to put the relevant text in vars, proceed to insert them on the BD, see:

$texto = trim(substr($mail->textPlain,20));
$a_texto=explode(":",$texto); // converte em array a string separados pelo :
echo $orc_name = str_replace("Endereço", " ", $a_texto[1] ) ;
echo $orc_postal = str_replace("Email", " ", $a_texto[2] ) ;
echo $orc_email = str_replace("Telefone", " ", $a_texto[3] ) ;
echo $orc_telf = str_replace("Dropdown Field", " ", $a_texto[4] ) ;
echo $orc_tservp = str_replace("Dropdown Field 2", " ", $a_texto[5] ) ;
echo $orc_tcasa = str_replace("Caixa de seleção", " ", $a_texto[6] ) ;

So far so good, but, now they changed the order of the placement of the 'fields' on the 'plaintext' string, see :

Clientes potenciais Nome: Testes Dropdown Field: Limpeza ou Tratamento de Pavimentos Dropdown Field 2: Escritório Telefone: 3321312312 Endereço: 312312312§3 Email: testes@testes.com Caixa de seleção: ✓ Verificado Testes Dropdown Field

With this change, all the above is useless, so my question to you is:

Can someone help me to figure a good solution in order to be able to extract the relevant info from that string? regardless of the place that they might be?

The relevant info is always after the ':' in the string, so:

Clientes potenciais Nome: Testes   --> The name field
Dropdown Field: Limpeza ou Tratamento de Pavimentos  --> The type field
Dropdown Field 2: Escritório  --> The type field 2
Telefone: 3321312312    -- > The Telephone field
Endereço: 312312312§3   --> The address postal code field
Email: testes@testes.com   --> The email field
Caixa de seleção: ✓ Verificado --> This is not necessary al all

NOTE : This is the new string, so as I said, I need that the 'fields' to be extracted regardless of the placement order they might be in the string.

I'm stuck in this, since I'm not a coder, but I do what I can, and this has been a chalange to me.

Thanks in advance to every one. JG

EDIT: After Mario's response I'm trying several regex using the function preg_match_all, the result to be achieved would be something like this:: Remember, this can be shuffled on the email plaintext string, but the structure doesn't change. Sd 'DB field name' and 'DB data value' always look as below, but not on the same order. This is the core of this post, to be able to gather the values, despite their order of appearence on the string.

Array ( 
[0] => Clientes potenciais Nome:  --->> DB field name - Always the same wthin the string
     [0] => Andreia Neto          --->> DB data value - Variable, according each email form
[1] => Dropdown Field:            --->> Same as above ...
     [0] => Manutenção Regular    --->> etc ...
[2] => Dropdown Field 2: 
     [0] => Apartamento 
[3] => Telefone: 
     [0] => 774943021 
[4] => Endereço: 
     [0] => 2770-089 
[5] => Email: 
     [0] => testes@gmail.com 
[6] => Caixa de seleção: 
     [0] => ✓ Verificado 
)

This are two strings for testing, which came from 2 emails received. Pls use them on testing if you wish:

String 1:

Clientes potenciais Nome: Andreia Neto Dropdown Field: Manutenção Regular Dropdown Field 2: Apartamento Telefone: 774943021 Endereço: 2770-089 Email: testes@gmail.com Caixa de seleção: ✓ Verificado

String 2

Clientes potenciais Nome: Jorge Gomes Endereço: 1234-123 Email: testes@email.com Telefone: 18745237 Dropdown Field: Limpeza ou Tratamento de Pavimentos Dropdown Field 2: Escritório Caixa de seleção: ✓ Verificado

Is this duable ??? I'm on it since 7am UTC of today, so any help will be much appreciated. Thanks

Comments