is_serialized() - WordPress 函数参考中文文档

返回介绍

is_serialized()

发布于 2017-09-11 字数2686 浏览 854 评论 0

is_serialized( string $data,  bool $strict = true )

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.


参数

$data

(string)
(Required)
Value to check to see if was serialized.

$strict

(bool)
(Optional)
Whether to be strict about the end of the string.

Default value: true


返回值

(bool) False if not serialized and true if it was.


源代码

File: wp-includes/functions.php

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' :
			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;
}

更新日志

Version 描述
2.0.5 Introduced.

相关函数

Used By

  • wp-admin/includes/template.php:
    _list_meta_row()
  • wp-includes/functions.php:
    maybe_unserialize()
  • wp-includes/functions.php:
    maybe_serialize()

User Contributed Notes

上一篇:is_search()

下一篇:is_serialized_string()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!