Using WordPress ‘is_serialized_string()’ PHP function

The is_serialized_string() WordPress PHP function checks whether serialized data is of string type.

Usage

is_serialized_string( $data );

Example:
Input: 's:13:"Hello, World!";'
Output: true

Parameters

  • $data (string) – The serialized data to be checked.

More information

See WordPress Developer Resources: is_serialized_string()

Examples

Basic usage of is_serialized_string()

This example checks if the given serialized data is of string type.

$data = 's:13:"Hello, World!";';
$result = is_serialized_string( $data );

echo $result; // Output: true

Handling non-string serialized data

This example shows how to handle non-string serialized data, such as arrays.

$array_data = 'a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}';
$result = is_serialized_string( $array_data );

echo $result; // Output: false

Checking for serialized string in a function

In this example, we create a function that uses is_serialized_string() to check if the provided data is a serialized string.

function check_serialized_string( $data ) {
    return is_serialized_string( $data );
}

$data = 's:10:"WordPress!";';
$result = check_serialized_string( $data );

echo $result; // Output: true

Using is_serialized_string() in a conditional statement

This example demonstrates using is_serialized_string() in a conditional statement to display a custom message.

$data = 's:12:"Hello there!";';

if ( is_serialized_string( $data ) ) {
    echo 'The data is a serialized string.';
} else {
    echo 'The data is not a serialized string.';
}

// Output: The data is a serialized string.

Working with invalid serialized data

This example shows how is_serialized_string() can handle invalid serialized data.

$invalid_data = 's:9:"Invalid data;';
$result = is_serialized_string( $invalid_data );

echo $result; // Output: false

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *