is_serialized

函数
is_serialized ( $data, $strict = true )
参数
  • (string) $data Value to check to see if was serialized.
    Required:
  • (bool) $strict Optional. Whether to be strict about the end of the string. Default true.
    Required:
    Default: true
返回值
  • (bool) False if not serialized and true if it was.
定义位置
相关方法
is_serialized_stringmaybe_serializewp_is_site_initializedmaybe_unserializeis_iterable
引入
2.0.5
弃用
-

is_serialized: 这个函数检查一个变量是否被序列化。如果该变量是序列化的,则返回真,否则返回假。

检查值是否被序列化。

如果$data不是一个字符串,那么返回值将总是false。序列化的数据总是一个字符串。

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 ( false === strpos( $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;
}

常见问题

FAQs
查看更多 >