5

I've got an array string serialised via PHP (Wordpress) similar to the following:

a:2:{i:0;s:12:"Sample array";i:1;a:2:{i:0;s:5:"Apple";i:1;s:6:"Orange";}}

How would I deserialise this string in C#?

If I paste the string into https://www.unserialize.com, it converts the structure fine.

Thanks!

5
  • 1
    I would suggest that you call a PHP script from C# that runs unserialize and then json_encode. You can then decode that in C#. Commented Jun 1, 2020 at 20:27
  • 1
    You could also look here if you want to implement it github.com/php/php-src/blob/… Commented Jun 1, 2020 at 20:35
  • 1
    PHP is a dynamically typed language. C# isn't. If you want the two to communicate, the best way is to use actual JSON (or another common format) so both can serialize and deserialize properly. Otherwise, you'll have to roll your own deserializer for this depending on the type you want created from it, such as IEnumerable or whatever. Commented Jun 1, 2020 at 20:36
  • 1
    You could even host this unserialize/json encoder on a webserver and just post the serialized string and get JSON back. Commented Jun 1, 2020 at 20:39
  • PHP's serialize() function should never have been exposed to the user space. You should never rely on it if you have a chance, and you should not, under any circumstances, unserialize user-submitted values. If you really want to do this bad thing, then the format is generally {type}:{size}:{value} with ; as a delimiter. Array entries are key-value pairs. Unprintable/special characters are not escaped or treated specially, and are even part of the format. Pray you never encounter a serialized object. GLWT. Commented Jun 1, 2020 at 21:32

1 Answer 1

2

You can use KaitaiStruct library.

Example(source):

// Parse a local file and get structure in memory
var data = PhpSerializedValue.FromFile("path/to/local/file.php_serialized_value");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.