Mark Fruhling Mark Fruhling - 5 months ago 12
PHP Question

Any Elegant Ideas on how to parse this Dataset?

I'm using PHP 5.3 to receive a Dataset from a web service call that brings back information on one or many transactions. Each transaction's return values are delimited by a pipe (

), and beginning/ending of a transaction is delimited by 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)

Doing a simple split on space doesn't work because of the space in the datetime stamp. I know regex 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 regex.


If each timestamp is going to have a Z at the end you can use positive lookbehind assertion to split on space only if it's preceded by a Z as:

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

Once you get the transactions, you can split them on | to get the individual parts.

Codepad link

Note that if your data has a Z followed a space anywhere else other than the timestamp, the above logic will fail. To overcome than you can split on space only if it's preceded by a timestamp pattern as:

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