我有一个包含许多记录的字符串,我想在||之后拆分每条记录每个记录再次用〜符号分隔。当我尝试这些时,我得到的唯一的第一条记录并非所有的记录。请你帮我解决这个问题
$data="promo~919441188673,9959045~2015-03-16~4:39 pm~ arey sollu shobhan gaa neeku pani ledaaa~Invalid Number||promo~919959095474~2015-03-16~4:40 pm~ Hi ra...how are you......~Message Sent||promo~919441188673~2015-03-13~3:51 pm~ Hi,This is from bhash sms ,and this is testing sorry for the ~Delivered";
list($someCode)=explode('||', $data);
//var_dump($data);
//var_dump($someCode);
list($type,$num,$date,$time,$msg,$status)=explode('~', $someCode);
var_dump($type);
var_dump($num);
var_dump($date);
var_dump($time);
var_dump($msg);
var_dump($status);
答案 0 :(得分:2)
这应该适合你:
(在这里,我使用array_map()
查看每个记录explode()
。然后我返回一个带有array_combine()
的组合数组
<?php
$data = "promo~919441188673,9959045~2015-03-16~4:39 pm~ arey sollu shobhan gaa neeku pani ledaaa~Invalid Number||promo~919959095474~2015-03-16~4:40 pm~ Hi ra...how are you......~Message Sent||promo~919441188673~2015-03-13~3:51 pm~ Hi,This is from bhash sms ,and this is testing sorry for the ~Delivered";
$someCode = array_map(function($v){
return array_combine(["type", "num", "date", "time", "msg", "status"], explode("~", $v));
}, explode('||', $data));
print_r($someCode);
?>
输出:
Array
(
[0] => Array
(
[type] => promo
[num] => 919441188673,9959045
[date] => 2015-03-16
[time] => 4:39 pm
[msg] => arey sollu shobhan gaa neeku pani ledaaa
[status] => Invalid Number
)
[1] => Array
(
[type] => promo
[num] => 919959095474
[date] => 2015-03-16
[time] => 4:40 pm
[msg] => Hi ra...how are you......
[status] => Message Sent
)
[2] => Array
(
[type] => promo
[num] => 919441188673
[date] => 2015-03-13
[time] => 3:51 pm
[msg] => Hi,This is from bhash sms ,and this is testing sorry for the
[status] => Delivered
)
)
答案 1 :(得分:0)
在第一个explode
之后你得到一个数组,而不是一个字符串,所以你需要迭代它并爆炸它的值:
list($someCode)=explode('||', $data);
foreach($someCode as $v){
list($type,$num,$date,$time,$msg,$status)=explode('~', $v);
var_dump($type);
var_dump($num);
var_dump($date);
var_dump($time);
var_dump($msg);
var_dump($status)
}
答案 2 :(得分:0)
使用爆炸功能可以实现您想要的效果。用这种方式爆炸
$pizza = "piece1||piece2||piece3||piece4||piece5||piece6";
$pieces = explode("||", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2