解析类似电子邮件的标题(类似于RFC822)

时间:2012-10-09 17:01:35

标签: php regex parsing rfc822

问题/问题

我想解析一个database of bot information。据说是similar to RFC822 messages

在我重新发明轮子并编写自己的解析器之前,我想我会看看是否还有其他东西可用。我偶然发现imap_rfc822_parse_headers(),这似乎完全符合我的要求。遗憾的是,IMAP扩展程序在我的环境中不可用。

我在网上和Stack Overflow上看到过很多替代品。不幸的是,它们都是为电子邮件而构建的,并且比我需要的更多...经常解析整个电子邮件并以特殊方式处理标题。我只想简单地将这些标题解析为有用的对象或数组。

是否有可用的imap_rfc822_parse_headers()的直接PHP版本,或类似的解析这样的数据的东西?如果没有,我会写自己的。

样本数据

robot-id: abcdatos
robot-name: ABCdatos BotLink
robot-from: no
robot-useragent: ABCdatos BotLink/1.0.2 (test links)
robot-language: basic
robot-description: This robot is used to verify availability of the ABCdatos
                   directory entries (http://www.abcdatos.com), checking
                   HTTP HEAD. Robot runs twice a week. Under HTTP 5xx
                   error responses or unable to connect, it repeats
                   verification some hours later, verifiying if that was a
                   temporary situation.
robot-history: This robot was developed by ABCdatos team to help
               working in the directory maintenance.
robot-environment: commercial
modified-date: Thu, 29 May 2003 01:00:00 GMT
modified-by: ABCdatos

robot-id:                       acme-spider
robot-name:                     Acme.Spider
robot-cover-url:                http://www.acme.com/java/software/Acme.Spider.html
robot-exclusion:                yes
robot-exclusion-useragent:      Due to a deficiency in Java it's not currently possible to set the User-Agent.
robot-noindex:                  no
robot-host:                     *
robot-language:                 java
robot-description:              A Java utility class for writing your own robots.
robot-history:                  
robot-environment:              
modified-date:                  Wed, 04 Dec 1996 21:30:11 GMT
modified-by:                    Jef Poskanzer

...

2 个答案:

答案 0 :(得分:2)

假设$data包含您上面粘贴的示例数据,这里是解析器:

<?php

/* 
 * $data = <<<'DATA'
 * <put-sample-data-here>
 * DATA;
 *
 */

$parsed  = array();
$blocks  = preg_split('/\n\n/', $data);
$lines   = array();
$matches = array();
foreach ($blocks as $i => $block) {
    $parsed[$i] = array();
    $lines = preg_split('/\n(([\w.-]+)\: *((.*\n\s+.+)+|(.*(?:\n))|(.*))?)/',
                        $block, -1, PREG_SPLIT_DELIM_CAPTURE);
    foreach ($lines as $line) {
        if(preg_match('/^\n?([\w.-]+)\: *((.*\n\s+.+)+|(.*(?:\n))|(.*))?$/',
                      $line, $matches)) {
            $parsed[$i][$matches[1]] = preg_replace('/\n +/', ' ',
                                                    trim($matches[2]));
        }
    }
}

print_r($parsed);

答案 1 :(得分:1)

消息MIME类型非常常见。解析器存在很多,但通常很难谷歌。我个人在这里使用正则表达式,如果格式有点一致。

例如,这两个将起到作用:

  // matches a consecutive RFC821 style key:value list
define("RX_RFC821_BLOCK", b"/(?:^\w[\w.-]*\w:.*\R(?:^[ \t].*\R)*)++\R*/m");

  // break up Key: value lines
define("RX_RFC821_SPLIT", b"/^(\w+(?:[-.]?\w+)*)\s*:\s*(.*\n(?:^[ \t].*\n)*)/m");

第一个突破消息/ *行的连贯块,第二个可用于拆分每个这样的块。它需要进行后处理,以便从持续的价值线中去除主要的压力。