Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse this string: a:10:{1:0;s:7:"default";i:1; ...?

How can I read strings like that? What do they mean?

a:10:{i:0;s:7:"default";i:1;s:8:"failsafe";i:2;s:4:"foaf";i:3;s:4:"ical";i:4;s:2:"js";i:5;s:4:"json";i:6;s:6:"opendd";i:7;s:3:"php";i:8;s:3:"rss";i:9;s:3:"xml";}

I've seen a lot of systems which use strings like that, stores it in the database and parse to get the values. How can I parse them?

Thanks.

like image 402
Danniel Magno Avatar asked Dec 04 '22 22:12

Danniel Magno


1 Answers

This is a serialized string. Look at the results of var_dump(unserialize()). It is NOT a valid JSON-formatted string (json_decode() will return null).

If you want to actually "read" it without unserializing it, you can see "a:10" means array with 10 indices. "i:0" means "index zero" and is semicolon-separated with the corresponding value ("s:7" is a string of length 7). The values are comma separated. Classes can also be serialized.

like image 108
Explosion Pills Avatar answered Dec 10 '22 10:12

Explosion Pills