0

I am trying to identify dates in some strings and the day of week and put the data into another array. My problem is that the data comes in array but not in a regular order.

Example #1 of data structure:

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
)

Example #2 of data structure:

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
)

Example #3 of data structure:

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

Example #4 of data structure:

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
)

Example #5 of data structure:

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
)

All of these structures are repetitive. I try to find a way to collect the lines for Mediu XXXX XXXX and Maxim XXXX XXXX. I want to create an array that should be look like this (let's take example #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
           )
)

Is there a possibility to achieve this? I don't have any regex idea to split data.

Thank you.

1 Answers1

2

What you can do is to transform the array in a string using implode, It will be more handy to find the weekdays and the dates:

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

Note that if you want you can avoid to capture the weekday and retrieve it with the date.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
  • Something is wrong because no text matches the pattern.. :-| I've imploded the array into text already... – Adrian Covaci Oct 28 '14 at 15:22
  • @AdrianCovaci: Where have you seen that? It's a full working code (tested). https://eval.in/210975 – Casimir et Hippolyte Oct 28 '14 at 15:25
  • Sorry! bad implementation. It works very very nice! Thank you a lot! – Adrian Covaci Oct 28 '14 at 15:46
  • If I would like to add a few lines in the preg match expression, I mean, other data like "Mediu" and "Maxim" (let's say I have more lines in the example array such as `[8] => Total (Pmed) => 180 170`),.. how do I alter the expression? Can you give me some idea? – Adrian Covaci Oct 28 '14 at 16:50