The is_serialized() WordPress PHP function checks if a given value is serialized or not.
Usage
is_serialized( $data, $strict )
Example:
Input:
$data = 'a:1:{s:3:"key";s:5:"value";}'; $is_serialized = is_serialized($data);
Output:
true
Parameters
- $data (string) – Required. The value to check if it is serialized.
- $strict (bool) – Optional. Whether to be strict about the end of the string. Default is true.
More information
See WordPress Developer Resources: is_serialized()
Examples
Check if a string is serialized
Check if a given string is serialized and print the result.
$data = 'a:1:{s:3:"key";s:5:"value";}'; if (is_serialized($data)) { echo 'The data is serialized.'; } else { echo 'The data is not serialized.'; }
Check if an array is serialized
Check if a given array is serialized and print the result.
$array = array('key' => 'value'); if (is_serialized($array)) { echo 'The array is serialized.'; } else { echo 'The array is not serialized.'; }
Check if serialized data with strict mode
Check if a given string is serialized with strict mode enabled and print the result.
$data = 'a:1:{s:3:"key";s:5:"value";'; if (is_serialized($data, true)) { echo 'The data is serialized (strict mode).'; } else { echo 'The data is not serialized (strict mode).'; }
Check if serialized data without strict mode
Check if a given string is serialized with strict mode disabled and print the result.
$data = 'a:1:{s:3:"key";s:5:"value";'; if (is_serialized($data, false)) { echo 'The data is serialized (non-strict mode).'; } else { echo 'The data is not serialized (non-strict mode).'; }
Check if serialized data with incorrect format
Check if a given string with incorrect format is serialized and print the result.
$data = 'a:1:{s:3:"key";s:5:"value"}'; if (is_serialized($data)) { echo 'The data is serialized.'; } else { echo 'The data is not serialized.'; }