Extract phone number from email body

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alexisc
    Senior Member
    • Aug 2019
    • 135

    Extract phone number from email body

    Please tell me how to extract a phone number in the format +1118889977 from the body of a email into a variable?
  • rabii
    Active Community Member
    • Jun 2016
    • 1250

    #2
    Hey

    You can use formula match with a regular expression to extract such numbers from the email, below code:

    PHP Code:
    // Use this to extract just one number from the body of the email
    $phone = string\match(body, '/\+\d{10,}/'); 
    
    PHP Code:
    // Use this to extract an array of all numbers from the body of the email
    $phones = string\matchAll('{token1} +1118889222 {token2} bar', '/\+\d{10,}/'); 
    

    Remember this regular expression will look for any number within the body that starts with (+) and has 10 digits.

    I hope this helps
    Rabii
    Web Dev

    Comment

    • alexisc
      Senior Member
      • Aug 2019
      • 135

      #3
      Originally posted by rabii
      Hey

      You can use formula match with a regular expression to extract such numbers from the email, below code:

      PHP Code:
      // Use this to extract just one number from the body of the email
      $phone = string\match(body, '/\+\d{10,}/'); 
      
      I hope this helps
      this works perfect, thanks!

      Comment


      • rabii
        rabii commented
        Editing a comment
        you are welcome
    • alexisc
      Senior Member
      • Aug 2019
      • 135

      #4
      stuck on the next step...

      step 1. Extracting the phone number, thanks to rabii for the help!

      PHP Code:
      $phone = string\match(body, '/\+\d{10,}/'); 
      
      Step 2. Find a lead record with this phone number

      PHP Code:
      ifThenElse($phone, record\exists('Lead', 'phoneNumber=', $phone), false); 
      

      step 3. I need to update email entity -> related record Email with Lead (extract ID of found record)

      Comment

      • rabii
        Active Community Member
        • Jun 2016
        • 1250

        #5
        Hey

        Not sure what the second step serves but i would just check the condition and update the email record (assuming this formula is for Email entity), see code below

        PHP Code:
        ifThen(
            $phone && record\exists('Lead', 'phoneNumber=', $phone), // if condition is true
            
            $leadId = record\findOne('Lead', 'createdAt', 'desc', 'phoneNumber=', $phone);
            $leadName = record\attribute('Lead', $leadId, 'name');
            
            // update the parent of the email with the found lead id
            record\update('Email', id, 'parentId', $leadId, 'parentName', $leadName);
        )

        I hope this helps
        Rabii
        Web Dev

        Comment

        • alexisc
          Senior Member
          • Aug 2019
          • 135

          #6
          I hope this helps [/QUOTE]

          It works perfect! Thanks a lot!

          Comment


          • rabii
            rabii commented
            Editing a comment
            you are welcome
        Working...