Any Elegant Ideas on how to parse this Dataset?

I am using PHP 5.3 to retrieve a dataset from a web service call that returns information about one or more transactions. Each transaction return value is limited by the pipe ( |), and the start / end of the transaction is limited to a space.

2109695|49658|25446|4|NSF|2010-11-24 13:34:00Z 2110314|45276|26311|4|NSF|2010-11-24 13:34:00Z 2110311|52117|26308|4|NSF|2010-11-24 13:34:00Z (etc)

Performing a simple split in space does not work due to the space in the date and time stamp. I know the regular expression well enough to know that there are always different ways to break this down, so I thought getting a few expert opinions would help me come up with the most airtight regular expression.

+3
source share
4

Z , lookbehind , Z as:

$transaction = preg_split('/(?<=Z) /',$input);

, |, .

Codepad

, Z, , , . , , :

$transaction = preg_split('/(?<=\d\d:\d\d:\d\dZ) /',$input);
+4

explode('|', $data)

+1

Z , "Z". . , Z .

+1

As others have said, if you know for sure that there will be no characters Zanywhere except for the date, you can simply do:

$records = explode('Z', $data);

But if you have them elsewhere, you need to get a little involved.

$regex = '#(?<=\d{2}:\d{2}:\d{2}Z)\s#i';
$records = preg_split($regex, $data, -1, PREG_SPLIT_NO_EMPTY);

Basically, this entry looks for the temporary part ( 00:00:00), followed by Z. Then it splits into the next space character ...

+1
source

Source: https://habr.com/ru/post/1777938/


All Articles