1

I'm serializing an object using serialize.js, I get this string:

a:4:{s:8:"option-1";i:0;s:8:"option-2";i:1;s:8:"option-3";i:0;s:8:"option-4";i:0;};

When I run unserialize in PHP, I get

unserialize() [function.unserialize]: Error at offset 5 of 123 bytes

How can I fix this problem?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
user1049961
  • 2,656
  • 9
  • 37
  • 69
  • 1
    Works fine for me! Please show the code how you unserialize your string – Rizier123 May 01 '15 at 17:40
  • There seem to be several libraries that provide a Javascript `serialize` function. Which one are you using? – Barmar May 01 '15 at 17:44
  • Hmmm, I just tried that, and it works if I do `unserialize('a:4:{s:8:"option-1";i:0;s:8:"option-2";i:1;s:8:"option-3";i:0;s:8:"option-4";i:0;};');`, however it doesn't work with `unserialize(trim($this->value()));` When I var_dump the `$this->value()`, I get the string... Lost here – user1049961 May 01 '15 at 17:45
  • The error which you have pasted is `Error at offset 5 of 123 bytes`, but the string is 84 bytes long. Are you sure this is the string you are having problems with? – VolenD May 01 '15 at 17:48
  • That's indeed strange - when I do `die(var_dump($this->value()))`, I get `string(123) "a:4:{s:8:"option-1";i:0;s:8:"option-2";i:1;s:8:"option-3";i:0;s:8:"option-4";i:0;};"` – user1049961 May 01 '15 at 17:51
  • You have some weird characters as you can see from your comment - <200c><200b> when pasted in `vim`. Can you try this: http://stackoverflow.com/questions/1176904/php-how-to-remove-all-non-printable-characters-in-a-string before `unserialize`? – VolenD May 01 '15 at 18:04

1 Answers1

-1

You most likely need to pass the serialized string through urlencode() before outputting.

To process it then, use urldecode() before unserialize().

Randomius
  • 252
  • 1
  • 2
  • 12