无法取消序列化`wp_options`表中的WordPress序列化值?

时间:2015-07-27 作者:Nam G VU

我有nav_menu_options 在我的Wordpress数据库中,如下所示。

284,\'nav_menu_options\',\'a:2:{i:0;b:0;s:8:\\"auto_add\\";a:0:{}}\'
我试图反序列化此值,但结果是空字符串-我在<my theme>/function.php 如下所示

echo \'TRY #1\'.\'<br>\';
$v = \'a:2:{i:0;b:0;s:8:\\"auto_add\\";a:0:{}}\';
$d = unserialize($v);
print_r($d);
echo \'TRY #1 end\'.\'<br>\';

echo \'TRY #2\'.\'<br>\';
$v =\'a:5:{s:9:"engine_id";a:1:{i:0;s:9:"300000225";}s:15:"transmission_id";a:1:{i:0;s:6:"257691";}s:5:"plant";a:1:{i:0;s:23:"Oshawa, Ontario, Canada";}s:15:"Manufactured in";a:1:{i:0;s:6:"CANADA";}s:22:"Production Seq. Number";a:1:{i:0;s:6:"151411";}}\';
$d = unserialize($v);
print_r($d);
echo \'TRY #2 end\'.\'<br>\';
上述代码的结果如下所示。

@1反序列化的Worpdress值结果为空。

@2示例有效序列化值将作为可读数组生成。

enter image description here

所以我的问题是Why can\'t we deserialize Wordpress serialized values? - 这也发布在wordpress上。组织机构here.

p、 s。

1) 对Wordpress代码的进一步研究表明,我们可以使用get_option(\'nav_menu_options\'); 命令

2) 的价值nav_menu_options 是从数据库复制的真实数据

enter image description here

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

Your problem is that serialized strings contains escape slashes that are not evaluated as such, because the wrapping quote is a single quote.

You are using:

$v = \'a:2:{i:0;b:0;s:8:\\"auto_add\\";a:0:{}}\'; // wrong

You have to use either

$v = "a:2:{i:0;b:0;s:8:\\"auto_add\\";a:0:{}}"; // ok

or

$v = \'a:2:{i:0;b:0;s:8:"auto_add";a:0:{}}\'; // ok

By the way, you should never manually unserialize values in WordPress database.

When you need to get an option (or a metadata, or anything that may be serialized) use WP functions: they unserialize the value when needed.

E.g. for options, use get_option.

If you are interested in how WordPress do unserialization see maybe_unserialize and is_serialized.

结束

相关推荐

如何将值添加到作为数组的wp_Options选项?

向中注册的期权添加期权值的最佳方法是什么wp_options 那是一个数组?对于phpmyadmin,我看到option\\u value字段是a:9:{i:3;s:15:\"value1\";i:6;s:5:\"value2\";i:7;s:21:\"value3\";i:8;s:15:\"value4\";i:9;s:7:\"value5\";i:10;s:4:\"value6\";i:11;s:5:\"value7\";i:12;s:8:\"value8\";i:13;s:7:\"value9\";