PHP中的日期正则表达式

时间:2013-10-15 16:47:19

标签: php regex

我在写一个正则表达式时遇到了一些麻烦(我不熟悉它)并且没有想到我的下一步应该是什么。我想要做的是使用PHP将一些文本块提取到一个数组中。文字如下:

Saturday, August 03, 2013  
DUMP                   Pickup:   LITTLE ROCK, AR  
Dest:  CALDWELL, TX   
HOPPER                Pickup:   BEECH GROVE, IN  
Dest:  TERRE HAUTE, IN  
Sunday, August 04, 2013  
HOPPER                Pickup:   JONESBORO, AR  
Dest:  BATTLE CREEK, MI  
LIVE BOTTOM         Pickup:   JONESBORO, AR  
Dest:  TAYLOR, MO

现在因为格式化我不能显示所有空格,例如DUMP和Pickup之间有大约3个制表符值空格。

所以我想要的是将包含日期的块放入数组中。使用^(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day,(.*)会为我提供包含日期的行,并使用((.|\n)*)代替(.*)选择所有内容。假设有多个条目,我怎样才能使这个正则表达式从日期一直到最后一个条目的选择,然后出现新的日期。

4 个答案:

答案 0 :(得分:0)

您可以使用此代码:

$s = <<< EOF
Saturday, August 03, 2013
DUMP Pickup: LITTLE ROCK, AR
Dest: CALDWELL, TX
HOPPER Pickup: BEECH GROVE, IN
Dest: TERRE HAUTE, IN
Sunday, August 04, 2013
HOPPER Pickup: JONESBORO, AR
Dest: BATTLE CREEK, MI
LIVE BOTTOM Pickup: JONESBORO, AR
Dest: TAYLOR, MO
EOF;
if (preg_match_all(
  "~(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day,(.+?)(?=\n(?:Mon|Tues|Wednes|Thurs|Fri|Satur|Sun)day,|$)~s", $s, $arr))
   var_dump($arr[0]);

<强>输出

array(2) {
  [0]=>
  string(126) "Saturday, August 03, 2013
DUMP Pickup: LITTLE ROCK, AR
Dest: CALDWELL, TX
HOPPER Pickup: BEECH GROVE, IN
Dest: TERRE HAUTE, IN"
  [1]=>
  string(126) "Sunday, August 04, 2013
HOPPER Pickup: JONESBORO, AR
Dest: BATTLE CREEK, MI
LIVE BOTTOM Pickup: JONESBORO, AR
Dest: TAYLOR, MO"
}

答案 1 :(得分:0)

使用正则表达式,我总是先在这里玩:http://regexpal.com/

然后你需要使用 - array preg_match

preg_match('/(^\w+day).+(\d{1,2})/', $str, $matches);

print_r($matches);

它应该打印你的数组:

 Saturday and dates ...

答案 2 :(得分:0)

每个相关的块都是它自己的数组,日期总是0,其他的也是可预测的。一点strstr()等或explode()可以从每一行获得类似的结果。

$lines = file($filename);
$chunks = array_chunk($lines, 5);
print_r($chunks);

Array
(
    [0] => Array
        (
            [0] => Saturday, August 03, 2013
            [1] => DUMP                   Pickup:   LITTLE ROCK, AR
            [2] => Dest:  CALDWELL, TX
            [3] => HOPPER                Pickup:   BEECH GROVE, IN
            [4] => Dest:  TERRE HAUTE, IN
        )

    [1] => Array
        (
            [0] => Sunday, August 04, 2013
            [1] => HOPPER                Pickup:   JONESBORO, AR
            [2] => Dest:  BATTLE CREEK, MI
            [3] => LIVE BOTTOM         Pickup:   JONESBORO, AR
            [4] => Dest:  TAYLOR, MO
        )

)

答案 3 :(得分:0)

我同意应该编写一个解析器,并且感到无聊,所以这就是我提出的:

function parse_( $str ) {
    $data = array();
    foreach( explode( "\n", $str ) as $line ) {
        if ( strpos( $line, ':' ) === false ) {
            $date = $line;
        }
        elseif( stripos( $line, 'pickup:' ) ) {
            $string = $line;
        }
        else {
            $data[$date][] = $string . ' -> ' . explode( ': ', $line )[1];
        }
    }
    return $data;
}

print_r( parse_( $str ) );

输出

Array
(
    [Saturday, August 03, 2013] => Array
        (
            [0] => DUMP Pickup: LITTLE ROCK, AR -> CALDWELL, TX
            [1] => HOPPER Pickup: BEECH GROVE, IN -> TERRE HAUTE, IN
        )

    [Sunday, August 04, 2013] => Array
        (
            [0] => HOPPER Pickup: JONESBORO, AR -> BATTLE CREEK, MI
            [1] => LIVE BOTTOM Pickup: JONESBORO, AR -> TAYLOR, MO
        )

)

使用strpos循环每一行以找出它的“类型”行。

如果您使用的是php&lt; 5.4(我相信)你必须改变最后的其他内容并添加爆炸数据的第一步。

http://ideone.com/heb4ty