Check to see if a string is serialized?

前端 未结 11 1625
情歌与酒
情歌与酒 2020-11-29 17:16

What\'s the best way to determine whether or not a string is the result of the serialize() function?

https://www.php.net/manual/en/function.serialize

11条回答
  •  攒了一身酷
    2020-11-29 18:06

    If the $string is a serialized false value, ie $string = 'b:0;' SoN9ne's function returns false, it's wrong

    so the function would be

    /**
     * Check if a string is serialized
     *
     * @param string $string
     *
     * @return bool
     */
    function is_serialized_string($string)
    {
        return ($string == 'b:0;' || @unserialize($string) !== false);
    }
    

提交回复
热议问题