0

我正在设置一个 PHP 脚本,该脚本将从维护帮助台通过管道发送电子邮件。这些电子邮件是从我们的客户公司使用的网络表单发送的,我无法控制。这些电子邮件在格式上是标准化的,但包含一个带有标签的列表,该标签来自网络表单。我想使用正则表达式来拆分这个列表并将标签和值放入一个数组中,我可以将其输入到我自己的数据库中。我有一个可行的解决方案,但我对正则表达式很陌生,我确信有更好/更有效的方法来做到这一点。

我可能收到的电子邮件示例:

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* 

它周围有更多样板,显然还有电子邮件标题等,但你明白了。每封电子邮件将仅包含一个列表,并且标签将保持不变,尽管我想在未来证明这一点,因此如果他们添加新字段,我将不需要更改我的代码。我想得到一个数组,例如:

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

虽然我可以确信格式不会改变,但每个表单都是用户输入的,因此可能是不可预测的,尤其是描述,它可能包含换行符。

这是我目前正在使用的代码:

// 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];

}

它有效,但它很难看,我相信有更好的方法。我遇到的主要问题是描述部分,因为我不能简单地搜索“:”之后的文本,直到换行符为止,因为描述本身可能包含换行符。

任何建议将不胜感激!

4

1 回答 1

0

仍然不是世界上最漂亮的东西,但它应该可以正常工作!

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]);

第一个preg_match_all执行您所说的并没有真正起作用,只是通过空格、冒号和换行符获取所有字段。

第二个替换第一个填写的可能错误的描述键。

于 2012-09-20T21:49:21.780 回答