如何:序列化$ data时反序列化($ data)?

时间:2014-04-24 17:34:47

标签: php serialization

我的MySQL数据库中的一些数据以序列化格式存储,例如a:1:{s:3:"url";s:70:"http://www.myurl.com/wp-content/uploads/2014/01/Crash_Test_Dummy-1.jpg";} 相同类型字段中的其他数据以非序列化的形式存储,例如。 http://www.myurl.com/wp-content/uploads/2014/01/Crash_Test_Dummy-1.jpg

现在,当我尝试使用$var=unserialize($data);检索数据且$data为上述未序列化字符串时,我收到错误Notice: unserialize(): Error at offset 0 of 69 bytes in ...

有没有快速的方法来序列化我的数据库中的所有反序列化字段?或者,有没有办法告诉服务器unserialize($data) if $data is serialized

谢谢!

1 个答案:

答案 0 :(得分:1)

WordPress喜欢在db ...上存储信息时使事情复杂化。

如果您在WP中开发代码,只需使用maybe_unserialize

否则,您可以复制该功能表单wp-includes/functions.php

/**
 * Unserialize value only if it was serialized.
 *
 * @since 2.0.0
 *
 * @param string $original Maybe unserialized original, if is needed.
 * @return mixed Unserialized data can be any type.
 */
function maybe_unserialize( $original ) {
    if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
        return @unserialize( $original );
    return $original;
}

/**
 * Check value to find if it was serialized.
 *
 * If $data is not an string, then returned value will always be false.
 * Serialized data is always a string.
 *
 * @since 2.0.5
 *
 * @param mixed $data Value to check to see if was serialized.
 * @param bool $strict Optional. Whether to be strict about the end of the string. Defaults true.
 * @return bool False if not serialized and true if it was.
 */
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;
    $length = strlen( $data );
    if ( $length < 4 )
        return false;
    if ( ':' !== $data[1] )
        return false;
    if ( $strict ) {
        $lastc = $data[ $length - 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 ( '"' !== $data[ $length - 2 ] )
                    return false;
            } elseif ( false === strpos( $data, '"' ) ) {
                return false;
            }
            // or else fall through
        case 'a' :
        case 'O' :
            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;
}