I have a problem that seems simple at first, but defeated my meager regex skills. I have a string that I need to convert to an array, and then process the values ββaccordingly, which is quite simple, but the format of the string cannot be changed (it is created elsewhere), and the logic of this confused me.
Line:
[6] [2] [3] 12.00; [5] [4]
Basically this is a set of identifiers and decimal values ββ(in this case id 3 == 12.00). The number of identifiers can change at any time, and decimal values ββcan be in any or all identifiers.
In an ideal world, I will have the following array:
Array ( [0] => Array ( [id] => 6 [num] => ) [1] => Array ( [id] => 2 [num] => ) [2] => Array ( [id] => 3 [num] => 12.00 ) Etc...
Do any of the regular expression masters help you, how can this be done with fewer curses than I could have achieved?
I have so far been able to extract the identifier using:
preg_match_all('@\[(.*?)\]@s', $string, $array);
and decimal places using:
preg_match_all('/([0-9]+[,\.]{1}[0-9]{2})/', $string, $array);
but lose the correlation between id and values.
source share