1

I have a string which contains a number of records and I want to split each record after || and each record is again separated by ~ symbol .when I am trying these I am getting the only first record not all the records.can you please help me out in this problem

$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);

3 Answers3

2

This should work for you:

(Here I go through each record with array_map() which I get with explode(). After this I return a combined array with 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);

?>

Output:

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
        )

)
Rizier123
  • 58,877
  • 16
  • 101
  • 156
0

After first explode you are getting an array, not a string, so you need to iterate over it and explode its values:

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)
}
n-dru
  • 9,285
  • 2
  • 29
  • 42
0

using explode function you can achive what you want. use explode this way

$pizza  = "piece1||piece2||piece3||piece4||piece5||piece6";
$pieces = explode("||", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Rajnikant Sharma
  • 536
  • 11
  • 27