检查字符串是否在 PHP 中序列化 [英] Check if string is serialized in PHP

查看:55
本文介绍了检查字符串是否在 PHP 中序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序构建 Redis 数据库的缓存层,我已经到了要处理数组的地步.

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.

我想知道是否有任何好的(高性能!)方法来控制字符串是否使用 PHP 进行序列化?

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

非常感谢!

推荐答案

$array = @unserialize($string);
if ($array === false && $string !== 'b:0;') {
    // woops, that didn't appear to be anything serialized
}

$string !== 'b:0;' 检查序列化字符串是否为值 false.如果此检查对您很重要,您可能需要修剪序列化字符串或以其他方式对其进行预处理以确保其有效.

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.

这篇关于检查字符串是否在 PHP 中序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆