1

嗨,我有一个带有这个值的 cookie

a:2:{i:0;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}i:1;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:4617;s:11:"category_id";s:3:"211";s:5:"price";d:1200;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:74:"Машка блуза,EXTERRA ГТЦ 1 спрат и CITY MALL 1спрат";s:12:"product_name";s:0:"";s:11:"thumb_image";s:42:"thumb_e069100cef8553637664fc695df55c66.JPG";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}

我需要读取该值并获取文本中数字 5196 位置的任何数字,就在“product_id”之后;一世: 。所以有人知道我如何使用 php 获得数字 5196?

4

4 回答 4

2

如果可能的话,我非常反对直接使用序列化数据。相反,我会使用 反序列化数据unserialize,然后从结果数组中获取值。

$data = 'a:2:{i:0;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196...';
$rslt = unserialize($data);

echo $rslt[0]["product_id"];

运行此代码:http ://codepad.org/JKYUS6yd

于 2012-12-05T21:24:47.623 回答
1

该字符串是一个序列化的对象。如果你反序列化它,你可以遍历它并找到 product_id = 5196 的数组,然后用它做你想做的事情,例如获取它的其他值:

$str = 'a:2:{i:0;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}i:1;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:4617;s:11:"category_id";s:3:"211";s:5:"price";d:1200;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:74:"Машка блуза,EXTERRA ГТЦ 1 спрат и CITY MALL 1спрат";s:12:"product_name";s:0:"";s:11:"thumb_image";s:42:"thumb_e069100cef8553637664fc695df55c66.JPG";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}';
$unserialObj = unserialize($str);
foreach($unserialObj as $item) {
    if($item['product_id'] == 5196) {
        echo $item['cateogry_id'] . ', ';
        echo $item['price'];
        //etc
    }
}
于 2012-12-05T21:31:20.983 回答
0
$str = 'a:2:{i:0;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:5196;s:11:"category_id";s:3:"209";s:5:"price";d:1;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:0:"";s:12:"product_name";s:4:"test";s:11:"thumb_image";s:0:"";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}i:1;a:17:{s:8:"quantity";i:1;s:10:"product_id";i:4617;s:11:"category_id";s:3:"211";s:5:"price";d:1200;s:3:"tax";s:5:"18.00";s:6:"tax_id";s:1:"1";s:11:"description";s:74:"Машка блуза,EXTERRA ГТЦ 1 спрат и CITY MALL 1спрат";s:12:"product_name";s:0:"";s:11:"thumb_image";s:42:"thumb_e069100cef8553637664fc695df55c66.JPG";s:3:"ean";s:0:"";s:10:"attributes";s:6:"a:0:{}";s:16:"attributes_value";a:0:{}s:6:"weight";s:6:"0.0000";s:9:"vendor_id";s:1:"0";s:5:"files";s:6:"a:0:{}";s:14:"freeattributes";s:6:"a:0:{}";s:25:"dependent_attr_serrialize";s:6:"a:0:{}";}}';
$ar = explode(";",$str);
$ar2 = explode(":",$ar[4]);
print_r($ar2[1]);
于 2012-12-05T21:25:01.160 回答
0

使用正则表达式:

$cookie = 'value from your post';
preg_match('/.*"product_id";i:([^;]+)/', $cookie, $matches);
$product_id = $matches[1];
于 2012-12-05T21:25:12.923 回答