2012-10-31 66 views
0

我有一个循环,返回了类似下面的字符串...如何从第二组引号中获取单词/内容?

  • S:20: “D111免费送货**”; S:4: “0.00”
  • S:32: “D111 3/5天发货服务***”; s:4:“6.99”
  • s:32:“D111 2/3天发货服务***”; s:4:“8.99”

我有正则表达式来从第一组引号中获取内容。

$shipping_name = preg_match('/"(.+?)"/', $shipp_option, $matches);

但我也想获得第二组引号内的号码,我会怎么做呢?

感谢

+0

是它序列字符串? – GBD

回答

3

explode();分隔符的字符串,然后unserialize()他们:

$string = 's:20:"D111 Free Delivery**";s:4:"0.00"'; 
$array = explode(';', $string); 
list($str, $number) = array_map('unserialize', $array); 
echo $str . ' ' . $number; 

你可以看到它在this demo,工作这对于你的三个测试案例,输出:

D111 Free Delivery** 
0.00 
D111 3/5 day delivery service*** 
6.99 
D111 2/3 day delivery service*** 
8.99 

编辑显示如何捕获每个字段在自己的变量。

+0

感谢您的回复,但我需要两个不同的变量。一个与字眼,另一个与数字 –

+0

这是一个非常简单的修改 - 你确定你不能自己找出一个呢? – nickb

+0

对不起,我正在密集。这正是我正在寻找的。谢谢你的帮助! –

0

爆炸!!!!

//inside your loop 
    $halves = explode(';', $shipp_option); 
    $first_half = explode(':', $halves[0]); 
    $second_half = explode(':', $halves[1]); 
    $shipping_name = trim($first_half[2], '"');//eg. D111 Free Delivery** 
    $shipping_price = trim($second_half[2], '"');//eg. 0.00 
//end inside your loop 

OR ...有点快:

//inside your loop 
    $shipp_arr = explode(';:', $shipp_option); 
    $shipping_name = trim($shipp_arr[2], '"');//eg. D111 Free Delivery** 
    $shipping_price = trim($shipp_arr[5], '"');//eg. 0.00 
//end inside your loop 
相关问题