Help parsing data

Web hosting discussion, programming, and shared and dedicated servers.
3 posts Page 1 of 1
by Steve Kucera » Fri Aug 24, 2012 10:12 pm
Hello,

I am writing a report in PERL for a SunShop Shopping Cart system. One of the fields is Base64 encoded, and the decoded data for a line item looks like this:

O:4:"item":4:{s:2:"id";i:3083;s:8:"quantity";d:6;s:7:"options";a:0:{}s:5:"regid";s:0:"";}

Looking for a little schooling in how to parse this data properly. The specific information I need is the quantity, and per other reports in the system the quantity on this item is 6.

Thanks in advance.
by toast0 » Fri Aug 24, 2012 10:37 pm
Steve,

This is php serialized data. There are a couple modules on CPAN that will parse that, but I'm not sure if they handle php objects like you have there (i always worked with just php arrays), and you should expect to find issues with large numbers/strings that look like numbers/floating point issues, the cases are a little tricky and the modules i have used didn't get them right. :/

So here's how I would parse that btw:

O -> this is an object, it has a
4 byte name, "item", and
4 properties, which each have a key and a value {
s, a string, with 2 bytes of data, "id" => i, an integer 3083;
s, a string, 8 bytes, "quantity" => d, a decimal (float)?, 6;
s, 7 bytes, "options" => a, an array, with 0 elements (empty) {}
s, 5, regid => s, 0, "" (empty string);
}

the format is odd: it's a little verbose, strings don't need byte counts and also quotes; and arrays and objects don't have semicolons after them, but everything else does, even though you wouldn't really need any of them, but I guess it was designed to look sort of human readable.
by Steve Kucera » Sun Aug 26, 2012 4:20 pm
Thanks Toast.
3 posts Page 1 of 1

Who is online

In total there are 24 users online :: 0 registered, 0 hidden and 24 guests (based on users active over the past 5 minutes)
Most users ever online was 999 on Mon May 10, 2021 1:02 am

Users browsing this forum: No registered users and 24 guests