What is this data format?

Bes

Bes

Soldato
Joined
18 Oct 2002
Posts
7,318
Location
Melbourne
Hi

I have a data format as follows, it does not seem to be a standard PHP or JSON serialized datatype. Neither method of unserializing it seems to work:

(This is a sample and returned from a payment gateway).

PHP:
a:2:{s:16:"authAmountString";s:10:"£33.39";s:11:"_SP_charEnc";s:5:"UTF-8";}

Any ideas what this is and how I can turn it into an array? (I am fetching it from a MySQL text field)

Thanks
 
Last edited:
Looks like it is the PHP serialize() format. Though the a:2 should be a:4 (it's an array with 4 elements).

Though having said that, it is missing the index elements (i:0 etc.)
 
Yes exactly it is missing the index elements so I cannot unserialize it,

Also I made a typo with the a: value, but still it's not a valid anything format it seems
 
Not sure what format that is, but couldn't you do a preg_match_all? ie:
Code:
preg_match_all('/"[^"\r\n]*"/', 'a:2:{s:16:"authAmountString";s:10:"£33.39";s:11:"_SP_charEnc";s:5:"UTF-8";}', $matches);

Would return a multidimensional array though, but is a quick bodge....
 
Not sure what format that is, but couldn't you do a preg_match_all? ie:
Code:
preg_match_all('/"[^"\r\n]*"/', 'a:2:{s:16:"authAmountString";s:10:"£33.39";s:11:"_SP_charEnc";s:5:"UTF-8";}', $matches);

Would return a multidimensional array though, but is a quick bodge....

Thanks but I am wondering should that work as-is? as it seems to not do anything. The syntax looks like it won't match up with the fields I have.

Thanks
 
$matches becomes a multidimentionsal array, so in this case $matches[0][1] is "£33.39" etc Do a print_r and it'll display the array.
As i said thought it's a bit of a bodge.
 
Last edited:
OK thanks getting there, this is what I get out, but it's not a multidimensional array... I cannot address it using $matches[0][1] for example

Array ( [0] => Array ( [0] => "authAmountString" [1] => "£34.49"))

Thanks
 
What is your exact code, as i've just tested it and it works perfectly fine. This is what's returned -
Code:
Array
(
    [0] => Array
        (
            [0] => "authAmountString"
            [1] => "£33.39"
            [2] => "_SP_charEnc"
            [3] => "UTF-8"
        )

)

Doing a print($matches[0][1]); returns "£33.39"

This is the full source code -
Code:
<?php
preg_match_all('/"[^"\r\n]*"/', 'a:2:{s:16:"authAmountString";s:10:"£33.39";s:11:"_SP_charEnc";s:5:"UTF-8";}', $matches);
print_r($matches);

//Print Indexes
print("<br />".$matches[0][0]);
print("<br />".$matches[0][1]);
print("<br />".$matches[0][2]);
print("<br />".$matches[0][3]);
?>
 
Last edited:
Got it working now thanks, was being a n00b before :D

Thanks again for all the help!
 
Back
Top Bottom