2012-12-05 100 views
1

在具体数值我有它获取从文本

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”之后;一世: 。所以有人有一个想法我怎么能得到数字5196使用PHP?

+0

格式总是一样吗?如果是这样,一些PHP字符串函数应该可以正常工作。找到该字符序列的位置。从结尾到下一个分号。 BOOM HEADSHOT – thatidiotguy

+0

@thatidiotguy yap它总是相同的 – Darko

回答

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

+0

谢谢伟大的工程! – Darko

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]); 
0

使用一个正则表达式:

$cookie = 'value from your post'; 
preg_match('/.*"product_id";i:([^;]+)/', $cookie, $matches); 
$product_id = $matches[1]; 
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 
    } 
}