Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从文本中获取特定值_Php_Parsing - Fatal编程技术网

Php 从文本中获取特定值

Php 从文本中获取特定值,php,parsing,Php,Parsing,嗨,我有一块有这个值的饼干 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:"e

嗨,我有一块有这个值的饼干

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”之后;i:。因此,有人知道如何使用php获取数字5196吗?

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

$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"];
使用正则表达式运行以下代码:

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

格式总是一样的吗?如果是这样,一些PHP字符串函数应该可以正常工作。查找该字符序列的位置。从它的结尾到下一个分号都取。繁荣HEADSHOT@thatidiotguy是啊,总是一样的
$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
    }
}