如何将这个字符串变成一个数组 - 字符串有 space 值也有
How to make this string into an Array - String has space and so do values
transactionid=67002 msisdn=12136018066 destination_msisdn=12136018066 country=Canada countryid=701 operator=1Canada operatorid=2350 reference_operator= originating_currency=CAD destination_currency=CAD product_requested=3 actual_product_sent=3 wholesale_price=3.61 retail_price=3.70 balance=9.96 sms_sent=no sms= cid1= cid2= cid3= pin_based=yes pin_option_1=key in : *105* (top-up code) # (press call button) pin_option_2=Click the WIND icon on your phone pin_option_3=to access the menu, and top up in a few quick steps pin_value=3 pin_code=9973 44700 7583 pin_ivr= pin_serial=5500000008 pin_validity=365 authentication_key=1455826552 error_code=0 error_txt=Transaction Good
当我们把它变成一个数组时,我们不想丢失像 pin_code
这样的值,它在数据中有 spaces,比如字符串的分隔符,它也是一个 space .到目前为止,这是我的代码:
$parsed = preg_split('/\s+/',$string);
$page_is = array_shift($parsed_url);
$getVars = array();
foreach($parsed as $argument) {
list($variable,$value) = explode("=",$argument); $getVars[$variable] = $value;
}
正则表达式的乐趣:
$regex = '/(\w+)=((?:.(?!\w+=))+)/';
preg_match_all($regex, $str, $matches, PREG_SET_ORDER);
var_dump($matches);
如果您有 php 5.5 或更高,您可以将这个樱桃放在顶部(底部)
$values = array_combine(array_column($matches, 1), array_column($matches, 2));
var_cump($values);
以下方法对我有用:
$str = "transactionid=67002 msisdn=12136018066 destination_msisdn=12136018066 country=Canada countryid=701 operator=1Canada operatorid=2350 reference_operator= originating_currency=CAD destination_currency=CAD product_requested=3 actual_product_sent=3 wholesale_price=3.61 retail_price=3.70 balance=9.96 sms_sent=no sms= cid1= cid2= cid3= pin_based=yes pin_option_1=key in : *105* (top-up code) # (press call button) pin_option_2=Click the WIND icon on your phone pin_option_3=to access the menu, and top up in a few quick steps pin_value=3 pin_code=9973 44700 7583 pin_ivr= pin_serial=5500000008 pin_validity=365 authentication_key=1455826552 error_code=0 error_txt=Transaction Good" ;
$parts = explode('=', $str);
$key = array_shift($parts);
$last_value = array_pop($parts);
foreach($parts as $part) {
preg_match('/[a-zA-Z0-9_]+$/', $part, $match);
$next_key = $match[0];
$value = str_replace($next_key, '', $part);
$array[$key] = $value;
$key = $next_key;
}
$array[$key] = $last_value;
$array = array_map('trim', $array);
var_dump($array);
transactionid=67002 msisdn=12136018066 destination_msisdn=12136018066 country=Canada countryid=701 operator=1Canada operatorid=2350 reference_operator= originating_currency=CAD destination_currency=CAD product_requested=3 actual_product_sent=3 wholesale_price=3.61 retail_price=3.70 balance=9.96 sms_sent=no sms= cid1= cid2= cid3= pin_based=yes pin_option_1=key in : *105* (top-up code) # (press call button) pin_option_2=Click the WIND icon on your phone pin_option_3=to access the menu, and top up in a few quick steps pin_value=3 pin_code=9973 44700 7583 pin_ivr= pin_serial=5500000008 pin_validity=365 authentication_key=1455826552 error_code=0 error_txt=Transaction Good
当我们把它变成一个数组时,我们不想丢失像 pin_code
这样的值,它在数据中有 spaces,比如字符串的分隔符,它也是一个 space .到目前为止,这是我的代码:
$parsed = preg_split('/\s+/',$string);
$page_is = array_shift($parsed_url);
$getVars = array();
foreach($parsed as $argument) {
list($variable,$value) = explode("=",$argument); $getVars[$variable] = $value;
}
正则表达式的乐趣:
$regex = '/(\w+)=((?:.(?!\w+=))+)/';
preg_match_all($regex, $str, $matches, PREG_SET_ORDER);
var_dump($matches);
如果您有 php 5.5 或更高,您可以将这个樱桃放在顶部(底部)
$values = array_combine(array_column($matches, 1), array_column($matches, 2));
var_cump($values);
以下方法对我有用:
$str = "transactionid=67002 msisdn=12136018066 destination_msisdn=12136018066 country=Canada countryid=701 operator=1Canada operatorid=2350 reference_operator= originating_currency=CAD destination_currency=CAD product_requested=3 actual_product_sent=3 wholesale_price=3.61 retail_price=3.70 balance=9.96 sms_sent=no sms= cid1= cid2= cid3= pin_based=yes pin_option_1=key in : *105* (top-up code) # (press call button) pin_option_2=Click the WIND icon on your phone pin_option_3=to access the menu, and top up in a few quick steps pin_value=3 pin_code=9973 44700 7583 pin_ivr= pin_serial=5500000008 pin_validity=365 authentication_key=1455826552 error_code=0 error_txt=Transaction Good" ;
$parts = explode('=', $str);
$key = array_shift($parts);
$last_value = array_pop($parts);
foreach($parts as $part) {
preg_match('/[a-zA-Z0-9_]+$/', $part, $match);
$next_key = $match[0];
$value = str_replace($next_key, '', $part);
$array[$key] = $value;
$key = $next_key;
}
$array[$key] = $last_value;
$array = array_map('trim', $array);
var_dump($array);