PHP,用字符串标识日期和星期几

时间:2014-10-28 13:20:23

标签: php preg-split

我正在尝试识别某些字符串中的日期和星期几,并将数据放入另一个数组中。 我的问题是数据是以数组形式出现的,但不是按正常顺序排列。

数据结构的示例#1:

Array (
    [3] => In ziua de Duminica 26.10.2014  Duminica 27.10.2013 // note both of dates in the same line
    [4] => Consum de energie electrica
    [5] => Mediu 6168 5407
    [6] => Maxim 7233 6552
)

数据结构示例#2:

Array (
    [3] => In ziua de Miercuri 15.10.2014 // note first line contains few more words
    [4] => Miercuri 16.10.2013
    [5] => Consum de energie electrica
    [6] => Mediu 6573 6747
    [7] => Maxim 7771 7892
)

数据结构示例#3:

Array (
    [4] => Marti 14.10.2014
    [6] => Marti 15.10.2013
    [7] => Consum de energie electrica
    [8] => Mediu 6453 6754
    [9] => Maxim 7551 7860
)

数据结构示例#4:

Array (
    [4] => Duminica 04.05.2014
    [6] => Duminica // note the line with only day of week and next line is the date
    [7] => 05.05.2013
    [8] => Consum de energie electrica
    [9] => Mediu 5265 4262
    [10] => Maxim 6318 4873
)

数据结构的示例#5:

Array (
    [4] => Miercuri12.02.2014 // note the missing space between the day of week and date
    [6] => Miercuri  13.02.2013
    [7] => Consum de energie electrica
    [8] => Mediu 7274 7434
    [9] => Maxim 8313 8401
)

所有这些结构都是重复的。我试图找到一种方法来收集Mediu XXXX XXXXMaxim XXXX XXXX的行。 我想创建一个看起来像这样的数组 (让我们以#1 为例):

Array (
    [0] => Array (
               [weekday] => Duminica
               [date]    => 26.10.2014
               [Mediu]   => 6168 // first value from line
               [Maxim]   => 7233 // first value from line
           )
    [1] => Array (
               [weekday] => Duminica
               [date]    => 27.10.2013
               [Mediu]   => 5407 // second value from line
               [Maxim]   => 6552 // second value from line
           )
)

是否有可能实现这一目标? 我没有任何正则表达式的想法来分割数据。

谢谢。

1 个答案:

答案 0 :(得分:2)

你可以做的是使用implode在字符串中转换数组,找到工作日和日期会更方便:

$arr = array (
    '3' => 'In ziua de Duminica 26.10.2014  Duminica 27.10.2013',
    '4' => 'Consum de energie electrica',
    '5' => 'Mediu 6168 5407',
    '6' => 'Maxim 7233 6552'
);

$pattern = '~
    (?<day0>  Duminica|Luni|Marti|Miercuri|Joi|Vineri|S[iî]mbata ) \h* 
    (?<date0> [0-9.]{10} ) \h*
    (?<day1>  \g<day0> ) \h*
    (?<date1> \g<date0> ) .*?
    Mediu\ (?<Med0> [0-9]+ )\ (?<Med1> [0-9]+ ) \h+
    Maxim\ (?<Max0> [0-9]+ )\ (?<Max1> [0-9]+ )
~xu';

$str = implode(' ', $arr);

if (preg_match($pattern, $str, $m)) {
    foreach (range(0,1) as $i) {
        $result[] = array(
            'weekday' => $m["day$i"],
            'date'    => $m["date$i"],
            'Mediu'   => $m["Med$i"],
            'Maxim'   => $m["Max$i"] );
    }
} else $result = false;

print_r($result);

请注意,如果您愿意,可以避免捕获工作日并使用日期检索它。