我试图理解Wordpress插件如何处理数据,当我从MySQL中取出它时,它就像这样:
a:1:{s:9:"home-team";a:6:{s:2:"id";s:9:"home-team";s:4:"slug";s:9:"home-team";s:4:"type";s:6:"select";s:4:"name";s:9:"Home Team";s:11:"description";s:0:"";s:4:"data";a:4:{s:7:"options";a:3:{s:60:"wpcf-fields-select-option-3892e2c3ad45e24dc7f47ff2ba880c33-2";a:2:{s:5:"title";s:13:"Chicago Bears";s:5:"value";s:1:"1";}s:60:"wpcf-fields-select-option-09fbd82bfa4142df6439c8e15d96dbfc-1";a:2:{s:5:"title";s:15:"New York Giants";s:5:"value";s:1:"2";}s:60:"wpcf-fields-select-option-7c7df972f933545b37c41ca249c686b4-1";a:2:{s:5:"title";s:15:"Oakland Raiders";s:5:"value";s:1:"3";}}s:8:"validate";a:1:{s:8:"required";a:3:{s:6:"active";s:1:"1";s:5:"value";s:4:"true";s:7:"message";s:22:"This Field is required";}}s:19:"conditional_display";a:2:{s:8:"relation";s:3:"AND";s:6:"custom";s:0:"";}s:16:"disabled_by_type";i:0;}}}
这个存储方式有名称吗?对我而言,它看起来有点像JSON,但当然这不是JavaScript。此外,有没有办法轻松清理它(通过使用在线工具),所以前几行看起来像这样:
a:1: {
s:9:"home-team";
a:6: {
s:2:"id";
s:9:"home-team";
s:4:"slug";
s:9:"home-team";
s:4:"type";
s:6:"select";
s:4:"name";
等等......等等......
答案 0 :(得分:4)
这是PHP序列化格式。
请参阅:http://php.net/manual/en/function.serialize.php
不确定如何获得与您拥有的格式化版本完全相同(但您可以轻松地将它们放在一起),但这是另一种了解序列化字符串中的内容的方法:
$test_string= 'a:1:{s:9:"home-team";a:6:{s:2:"id";s:9:"home-team";s:4:"slug";s:9:"home-team";s:4:"type";s:6:"select";s:4:"name";s:9:"Home Team";s:11:"description";s:0:"";s:4:"data";a:4:{s:7:"options";a:3:{s:60:"wpcf-fields-select-option-3892e2c3ad45e24dc7f47ff2ba880c33-2";a:2:{s:5:"title";s:13:"Chicago Bears";s:5:"value";s:1:"1";}s:60:"wpcf-fields-select-option-09fbd82bfa4142df6439c8e15d96dbfc-1";a:2:{s:5:"title";s:15:"New York Giants";s:5:"value";s:1:"2";}s:60:"wpcf-fields-select-option-7c7df972f933545b37c41ca249c686b4-1";a:2:{s:5:"title";s:15:"Oakland Raiders";s:5:"value";s:1:"3";}}s:8:"validate";a:1:{s:8:"required";a:3:{s:6:"active";s:1:"1";s:5:"value";s:4:"true";s:7:"message";s:22:"This Field is required";}}s:19:"conditional_display";a:2:{s:8:"relation";s:3:"AND";s:6:"custom";s:0:"";}s:16:"disabled_by_type";i:0;}}}';
$unser = unserialize( $test_string);
print_r ( $unser );
将显示:
Array
(
[home-team] => Array
(
[id] => home-team
[slug] => home-team
[type] => select
[name] => Home Team
[description] =>
[data] => Array
(
[options] => Array
(
[wpcf-fields-select-option-3892e2c3ad45e24dc7f47ff2ba880c33-2] => Array
(
[title] => Chicago Bears
[value] => 1
)
[wpcf-fields-select-option-09fbd82bfa4142df6439c8e15d96dbfc-1] => Array
(
[title] => New York Giants
[value] => 2
)
[wpcf-fields-select-option-7c7df972f933545b37c41ca249c686b4-1] => Array
(
[title] => Oakland Raiders
[value] => 3
)
)
[validate] => Array
(
[required] => Array
(
[active] => 1
[value] => true
[message] => This Field is required
)
)
[conditional_display] => Array
(
[relation] => AND
[custom] =>
)
[disabled_by_type] => 0
)
)
)
答案 1 :(得分:1)
这就是WordPress在数据库中存储数组和对象的方式。来自文章WordPress serializes options and meta for you
在最基本的用例中,serialization是一种直接在数据库中存储数组和对象的方法,只能存储数字,文本和日期。序列化采用数组并将其转换为序列化字符串。例如:
$data = array( 'apple', 'banana', 'orange' ); echo serialize( $data ); // Result is a string we can unserialize into an array: // a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}
WordPress有一些帮助函数,而不是
serialize()
和unserialize()
-maybe_serialize()
和maybe_unserialize()
。第一个只序列化需要序列化的数据 - 数组和对象 - 第二个仅反序列化已经序列化的数据。 (我们有很多方便的功能。)
你提到“当我把它从MySQL中拉出来时就像这样”,所以你可能没有使用捆绑的函数来提取数据,比如get_option()
,{{ 3}}和get_post_meta()。这些函数负责对数据进行反序列化。
值得注意的是,您应该使用get_user_meta()
之类的工具来搜索/替换数据库。因为它负责替换序列化数据中的字符串。
你不能简单地改变:
a:3:{i:0;s:5:"apple";i:1;s:6:"banana";i:2;s:6:"orange";}
到
a:3:{i:0;s:5:"grapefruit";i:1;s:6:"banana";i:2;s:6:"orange";}
因为它应该是s:10:"grapefruit";
,是字符串中字符数的10。