Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:41:20+00:00 2026-06-11T15:41:20+00:00

I’m setting up a PHP script that will have emails piped to it from

  • 0

I’m setting up a PHP script that will have emails piped to it from a maintenance help desk. These emails are sent from a web form used by our client company, which I have no control over. The emails are standarised in format but contain a list with labels that is fed from the web form. I want to use regular expressions to split out this list and put the labels and values into an array, which I can feed into my own database. I have got a working solution on the go but I’m very new at regex and I’m sure there is a better / more efficient way to do it.

An example of an email that I may recieve:

Dear *MY COMPANY*,

A new job has been raised, please see details below.
If you are unable to action this job request, please notify the Maintenance Help Desk on xxx-xxxx as soon as possible.

    Job Type: Man In Van
    Job Code: 1462399
    Due Date: 27/09/2012 07:21:10
    Response Time: Man In Van
    Pub Number: 234
    Pub Name: pub name, location
    Pub Address: 123 somewhere, some place XX1 7XX
    Pub Post Code: XX1 7XX
    Pub Telephone Number: xxx xxxx
    Placed By: Ben
    Date/time placed: 20/09/2012 07:21:10
    Trade Type: Man In Van
    Description: List of jobs emailed by Chris, carried out by Martin Baker. No callout on system currently, although jobs already completed, just need signing off.


    For any queries, please either contact the pub directly, telephone the Maintenance Help Desk on xxx-xxxx or reply to this e-mail.

Many Thanks
*CLIENT COMPANY* 

There is more boilerplate around it, and obviously the email headers and such, but you get the idea. Each email will only contain one list, and the labels will remain the same, although I would like to future proof it so should they add new fields I will not need to change my code. I want to end up with an array such as:

$job['Job Type'] = Man in van
$job['Job Code'] = 1462399
...
$job['Description'] = List of all jobs emailed ... just need signing off.

Although I can be confident that the format will not change, every form is user input and as such may be unpredictable, particularly the description, which may contain line breaks.

This is the code I am using at the moment:

// Rip out the job details from the email
preg_match_all('/job type\:.*description\:.*\s{3}F/is', $the_email, $jobs);

    for each job returned (should always be one but hey)
    foreach($jobs[0] as $job_details) {

// Get the variables from the job description
    preg_match_all('/(\w[^\:]*)\: ([\w\d][^\*]+)/i', $job_details, $the_vars);

}

    // For each row returned, put into an array with the first group as the key and the second as the value
for ($i=0; $i<count($the_vars[0]); $i++) {

    $arr[$the_vars[1][$i]] = $the_vars[2][$i];

}

It works, but it is ugly and I’m sure there is a better way. The main problem I am having is the description section, as I cannot simply search for the text following the ‘:’ sign up until a line break, as the description itself may contain line breaks.

Any advice would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:41:21+00:00Added an answer on June 11, 2026 at 3:41 pm

    Still not the prettiest thing in the world but it should work just fine!

    preg_match_all('/\s{3}[ ]*([^:]+): ([^\n]+)/', $subject, $matches);
    $job = array_combine($matches[1], $matches[2]);
    
    preg_match_all('/Description\: (.*)\s{3}For any queries/is', $subject, $match);
    $job['Description'] = trim($match[1][0]);
    

    The first preg_match_all does what you said doesn’t really work, just grabs all of the fields by whitespace, colon and newline.

    The second one replaces the potentially erroneous Description key that the first one filled in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm trying to create an if statement in PHP that prevents a single post
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.