I have the data saved using a plugin in this format:

a:7:{s:6:'Sunday ';a:3:{s:6:'active ';s:3:'yes ';s:5:'start ';s:5:'08: 00 ';s:3:'end ';s:5:'13: 00 ';}
    s:6:'Monday ';a:3:{s:6:'active ';s:3:'yes ';s:5:'start ';s:5:'08: 00 ';s:3:' end ';s:5:'17: 00 ';}
    s:7:'Tuesday ';a:3:{s:6:' active ';s:3:'yes ';s:5:'start ';s:5:'08: 00 ';s:3:'end ';s:5:'17: 00 ';}
    s:9:'Wednesday ';a:3:{s:6:'active ';s:3:'yes ';s:5:'start ';s:5:'08: 00 ';s:3:'end ';s:5:'17: 00 ';}
    s:8:'Thursday ';a:3:{s:6:'active ';s:3:'yes ';s:5:'start ';s:5:'08: 00 ';s:3:'end ';s:5:'17: 00 ';}
    s:6:'Friday ';a:3:{s:6:'active ';s:3:'yes ';s:5:'start ';s:5:'08: 00 ';s:3:'end ';s:5:'17: 00 ';}
    s:8:'Saturday ';a:3:{s:6:'active ';s:3:'yes ';s:5:'start ';s:5:'08: 00 ';s:3:'end ';s:5:'13: 00 ';}}

How can I extract this data using php or any WordPress functions? I have already tried unserialize(), maybe_unserialize() json_decode . But all I get is null in the end.

  • Who knows!!! Have you tried json_decode()? – Nathan Powell Jan 30 '16 at 7:16
  • The problem with this question is: no one knows what you are trying to do. Give us more information. – Nathan Powell Jan 30 '16 at 7:20
  • Yes I have tried json_encode() but it gives me null in response. The plugin saves the data in this format in database. SO what I need to to extract this data in array to display on template. – Sachin Stellen Jan 30 '16 at 7:26
  • This is not a valid json, as I validated this. But there should be a way to get the values inside? – Sachin Stellen Jan 30 '16 at 7:27
  • I tried to parse your JSON into this and got an error that SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data. Please correct it first. – AddWeb Solution Pvt Ltd Jan 30 '16 at 7:27

The comments and answers are correct, it is no JSON but indeed a serialized array. The reason you're having trouble unserializing it is because of the quotes inside the serialized data. David Walsh wrote a neat article about this. The problem is that you can't simply go into the database and remove these single quotes because the serialized value keeps track of the number of characters in the string (s:6) so you would also need to update that.

Here's what David Walsh suggests to get around this issue in the future:

//to safely serialize
$safe_string_to_store = base64_encode( serialize( $multidimensional_array ) );

//to unserialize...
$array_restored_from_db = unserialize( base64_decode( $encoded_serialized_string ) );
  • This answer is not good. We have codex.wordpress.org/Function_Reference/is_serialized for that. – Nathan Powell Jan 30 '16 at 7:59
  • @NathanPowell The data is clearly serialized but corrupt which is why it can't be unserialized. is_serialized() only checks if it's a string not if it's valid data. The point of base64 encoding is if you need to store things with singe/double quotes. – Howdy_McGee Jan 30 '16 at 8:03
  • Granted, but that is not the clear answer here. We have no idea how this "format" was achieved. I like to think WordPress doesn't need to be hacked when answering here. – Nathan Powell Jan 30 '16 at 8:09
  • 1
    I'm sorry @Howdy_McGee, I respect your answer. – Nathan Powell Jan 30 '16 at 9:02
  • 1
    Looking again at this tonight. Thanks for the great article @Howdy_McGee. I most certainly see the issue. The serialized array is not escaped properly when saved, therefore it comes back with corruption. I think it is important to note that this is a "hack" to extract what could be potentially malicious code that is saved. If best practices are used to save the data in the above array, this "hack" would not need to be used. – Nathan Powell Feb 3 '16 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.