简体   繁体   中英

Extract customer data from email

I am getting my emails through imap with php, and I get in return the message's body. I need to extract some customer info from this message, like Name, E-mail, Dates and related apartment.

--_av-ml1j40HUdHU5gAwKk3Xhrg
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

       --- Para responder este email, clique no botão azul abaixo ---     


Fernando, há um interessado em alugar seu imóvel!        
  COBERTURA Fantástica de FRENTE MAR! Para 6, Wifi, Ar C., Churrasq
<https://www.temporadalivre.com/conversas?id_hash=Guoqweasdd8xpu9jqlGN> 
     *Dados do Interessado*

  *Vagner Fulano*
 *vagner.spqweor01@gmail.com*

 *Período*

 21 de Dezembro de 2019 a 
26 de Dezembro de 2019
(*5 diárias*) 

 *Hóspedes*

  2 adultos, 1 criança

 *Telefone*

 44999238255

 *País*

 Brasil

 (localização por estimativa)

 *Mensagem*

 -
      Clique aqui para responder →
<https://www.temporadalivre.com/conversas?id_hash=GuoieVQACd8xpu9jqlGN>    

Above is the partial string that I can get from my emails. Or maybe, this other part can help:

                    <table style="border-spacing: 0; border-collapse: collapse; vertical-align: top; text-align: left; width: 100%; padding: 0;"><tr style="vertical-align: top; text-align: left; padding: 0;" align="left"><th style="color: #0a0a0a; font-family: Helvetica, Arial, sans-serif; font-weight: normal; text-align: left; line-height: 150%; font-size: 16px; margin: 0; padding: 0;" align="left">
                         <div style="color: #daa520;">
                          <b>Dados do Interessado</b><br /></div>
                           <div style="color: #808000;">
                              <b>Vagner Splendor</b><br />
                              <span style="font-family: Helvetica, Arial, sans-serif; font-weight: normal; text-align: left; line-height: 1.3; text-decoration: none; margin: 0; padding: 0; font-size: 0.8em"><i>vagner.splendor01@gmail.com</i></span><br /><br /></div>


                           <div style="color: #daa520;"><b>Período</b><br /></div>
                          <div style="color: #808000;">21 de Dezembro de 2019 a <br>26 de Dezembro de 2019<br>(<b>5 diárias</b>) <br /><br /></div>

                           <div style="color: #daa520;"><b>Hóspedes</b><br /></div>
                          <div style="color: #808000;"> 2 adultos, 1 criança<br /><br /></div>

                           <div style="color: #daa520;"><b>Telefone</b><br /></div>
                          <div style="color: #808000;">44995528255</div><br />

                            <div style="color: #daa520;"><b>País</b><br /></div>
                            <div style="color: #808000;">Brasil<br /></div>
                            <div style="color: #808000; font-size: 14px">(localização por estimativa)<br /><br /></div>

                          <div style="color: #daa520;"><b>Mensagem</b><br /></div>
                          <div style="color: #808000;">-</div>
                        </th>
                      </tr></table></th>
                </tr></tbody></table>

Would guys have a solution for me? I have no idea how I can use regex for that matter.

This is quite unstructured, and I'm positive that we might not end up with anything interesting, approaching solving this problem, if possible, maybe we should look for ways to change this problem, yet if that option is not to exercise, an example expression would be:

<i>(.+?)<\/i>|(<div style="color: #808000;">)[\s\S]*?<b>(.+?)<\/b><br

which captures the email and name using these two capturing groups, (.+?) , and the rest can be scripted.

Demo

Test

$re = '/<i>(.+?)<\/i>|(<div style="color: #808000;">)[\s\S]*?<b>(.+?)<\/b><br/m';
$str = '                    <table style="border-spacing: 0; border-collapse: collapse; vertical-align: top; text-align: left; width: 100%; padding: 0;"><tr style="vertical-align: top; text-align: left; padding: 0;" align="left"><th style="color: #0a0a0a; font-family: Helvetica, Arial, sans-serif; font-weight: normal; text-align: left; line-height: 150%; font-size: 16px; margin: 0; padding: 0;" align="left">
                         <div style="color: #daa520;">
                          <b>Dados do Interessado</b><br /></div>
                           <div style="color: #808000;">
                              <b>Vagner Splendor</b><br />
                              <span style="font-family: Helvetica, Arial, sans-serif; font-weight: normal; text-align: left; line-height: 1.3; text-decoration: none; margin: 0; padding: 0; font-size: 0.8em"><i>vagner.splendor01@gmail.com</i></span><br /><br /></div>


                           <div style="color: #daa520;"><b>Período</b><br /></div>
                          <div style="color: #808000;">21 de Dezembro de 2019 a <br>26 de Dezembro de 2019<br>(<b>5 diárias</b>) <br /><br /></div>

                           <div style="color: #daa520;"><b>Hóspedes</b><br /></div>
                          <div style="color: #808000;"> 2 adultos, 1 criança<br /><br /></div>

                           <div style="color: #daa520;"><b>Telefone</b><br /></div>
                          <div style="color: #808000;">44995528255</div><br />

                            <div style="color: #daa520;"><b>País</b><br /></div>
                            <div style="color: #808000;">Brasil<br /></div>
                            <div style="color: #808000; font-size: 14px">(localização por estimativa)<br /><br /></div>

                          <div style="color: #daa520;"><b>Mensagem</b><br /></div>
                          <div style="color: #808000;">-</div>
                        </th>
                      </tr></table></th>
                </tr></tbody></table>';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

// Print the entire match result
var_dump($matches);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM