Emery King
Emery King

Reputation: 3534

What is this encoding called?

I see it in Wordpress in the database and i now see similar in a cookie. What kind of parser parses this:

a:4:{s:14:"clientsorderby";s:9:"firstname";s:12:"clientsorder";s:4:"DESC";s:13:"ordersorderby";s:2:"id";s:11:"ordersorder";s:4:"DESC";}

I get it, its a=array:x=number of children s=string:x=number of characters.

Is there a parser built into php for this kind of thing? why do they use this method?

Upvotes: 3

Views: 82

Answers (1)

ziad-saab
ziad-saab

Reputation: 20239

It's PHP's built-in serialize(), which can be "decoded" with unserialize()

Here is an example:

$serialized = 'a:4:{s:14:"clientsorderby";s:9:"firstname";s:12:"clientsorder";s:4:"DESC";s:13:"ordersorderby";s:2:"id";s:11:"ordersorder";s:4:"DESC";}';
$unserialized = unserialize( $serialized);

var_dump( $unserialized);

Output:

array(4) {
  ["clientsorderby"]=>
  string(9) "firstname"
  ["clientsorder"]=>
  string(4) "DESC"
  ["ordersorderby"]=>
  string(2) "id"
  ["ordersorder"]=>
  string(4) "DESC"
}

Upvotes: 6

Related Questions