unserialize:需要调试到屏幕,人类可读

时间:2013-01-01 01:29:53

标签: php deserialization

我正在使用MyBB,一个名为NewPoints Shop的模块。

这是序列化字段:

$barnInfo['newpoints_items'] = "a:7:{i:0;s:1:"2";i:1;s:1:"2";i:2;s:1:"2";i:3;s:1:"2";i:4;s:1:"5";i:5;s:1:"7";i:6;s:1:"7";}" 

// the deserialized field represents this data:
4 Lumber (Item ID 2)
1 Mushroom (Item ID 5)
2 Carrot (Item ID 7)
(And perhaps 0 of every other item - there are 9 items: Item ID 1, 2, 3, ...7, 8, 9)

在他们的代码中,他们打电话给

$items = unserialize($barnInfo['newpoints_items']);

我很难操纵这些数据。我想如果我可以将反序列化的数据打印到屏幕上,我可以弄清楚如何使用它。但我无法弄清楚如何打印任何有意义的东西。我玩过print_r但没有成功。

// displays '1' , quite unhelpful!
print_r(unserialize($barnInfo['newpoints_items'] ))
// I tried and got very confusing results that don't seem to correspond at all http://blog.tanist.co.uk/files/unserialize/index.php

问题:如何将反序列化的数据打印到屏幕上,以便我可以弄清楚它是什么?

* 答案:* MyBB在将内容打印到屏幕上有点棘手,但这样做有效:"<pre>" . htmlspecialchars(print_r($items, true)) . "</pre> ... "

1 个答案:

答案 0 :(得分:1)

假设$barnInfo['newpoint_items']实际上包含序列化数据,那么将print_r包裹在pre标记中会使其更具可读性。

echo '<pre>';
print_r(unserialize($barnInfo['newpoint_items']));
echo '</pre>';