Check if string is serialized in PHP
Asked Answered
M

3

6

I am in the middle of building a cache layer for the Redis DB to my application and I have come to the point where's it's about to take care of arrays.

I wonder if there's any good (high performance!) way of controlling an string to be serialized or not with PHP?

Thanks a lot!

Monochord answered 20/5, 2010 at 22:11 Comment(3)
What do you mean? a string intern pool? PHP doesn't have that (yet)Crudity
I can't really tell what you're looking for from the wording of your question. The title made it sound like you just wanted a way to check if any given string is a serialized representation of something?Bulrush
Does this answer your question? Check to see if a string is serialized?Rudolph
E
16
$array = @unserialize($string);
if ($array === false && $string !== 'b:0;') {
    // woops, that didn't appear to be anything serialized
}

The $string !== 'b:0;' checks to see if the serialized string may have been the value false. If this check is important to you you may want to trim the serialized string or otherwise preprocess it to make sure this works.

Eakins answered 20/5, 2010 at 22:18 Comment(3)
you don't have to take the risk, you can check error_get_lastCrudity
@Crudity This may or may not be useful, as it can be hard to tell whether the last error was really thrown here or sometime earlier. A better way may be to look at the serialized string to see if it looks like a single serialized false. Anyway, who serializes false values? ;)Eakins
Hi Deceze! Sounds great, I will definitely try that out! Have a great weekend!Monochord
G
0

For those looking for alternative, this code worked for me as helper function for Laravel framework and then you can call it anywhere.

if(!function_exists('isSerialized')){
    function isSerialized($string){
        $tempString = '';
        $array = @unserialize($string);
        if(is_array($array)){
            foreach ($array as $k=>$i){
                //do something with data
                $tempString .= $k . $i['something'];
            }
        } else {
            $tempString = $string;
        }
        return $itemString;
    }
}
Guay answered 27/7, 2022 at 9:41 Comment(0)
I
0

You can use the below function to check if a string weather serialized or not. The link of the function: https://developer.wordpress.org/reference/functions/is_serialized/

function is_serialized( $data, $strict = true ) {
        // If it isn't a string, it isn't serialized.
        if ( ! is_string( $data ) ) {
            return false;
        }
        $data = trim( $data );
        if ( 'N;' === $data ) {
            return true;
        }
        if ( strlen( $data ) < 4 ) {
            return false;
        }
        if ( ':' !== $data[1] ) {
            return false;
        }
        if ( $strict ) {
            $lastc = substr( $data, -1 );
            if ( ';' !== $lastc && '}' !== $lastc ) {
                return false;
            }
        } else {
            $semicolon = strpos( $data, ';' );
            $brace     = strpos( $data, '}' );
            // Either ; or } must exist.
            if ( false === $semicolon && false === $brace ) {
                return false;
            }
            // But neither must be in the first X characters.
            if ( false !== $semicolon && $semicolon < 3 ) {
                return false;
            }
            if ( false !== $brace && $brace < 4 ) {
                return false;
            }
        }
        $token = $data[0];
        switch ( $token ) {
            case 's':
                if ( $strict ) {
                    if ( '"' !== substr( $data, -2, 1 ) ) {
                        return false;
                    }
                } elseif ( ! str_contains( $data, '"' ) ) {
                    return false;
                }
                // Or else fall through.
            case 'a':
            case 'O':
            case 'E':
                return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
            case 'b':
            case 'i':
            case 'd':
                $end = $strict ? '$' : '';
                return (bool) preg_match( "/^{$token}:[0-9.E+-]+;$end/", $data );
        }
        return false;
    }
Icono answered 30/11, 2023 at 17:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.