Home >Backend Development >PHP Tutorial >How to Unserialize a PHP Serialized String?

How to Unserialize a PHP Serialized String?

Susan Sarandon
Susan SarandonOriginal
2024-12-05 00:30:10933browse

How to Unserialize a PHP Serialized String?

Understanding and Unserializing Serialized Strings

In your case, you have a serialized string of the following format:

a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}

To answer your questions:

What kind of string is this?

This is a serialized string, a representation of an array in a specific format. It encodes information about the array, including key-value pairs and the types of values.

How can I unserialize it and get the array out of it?

You can unserialize this string using PHP's unserialize() function. Here's an example:

$str = 'a:2:{i:0;s:7:"Abogado";i:1;s:7:"Notario";}';
$result = unserialize($str);

The output of print_r($result) will be:

Array ( [0] => Abogado [1] => Notario )

Important Notes:

  • Serialized strings may return FALSE if there's an error or if they represent a serialized FALSE value. Comparing the string with serialize(false) or catching the E_NOTICE exception can handle these cases.
  • For security reasons, do not deserialize untrusted user input. Unserialization can execute code, and malicious users may exploit this. Consider using a safer data interchange format like JSON instead.

The above is the detailed content of How to Unserialize a PHP Serialized String?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn