Search code examples
phparraysexplode

Why explode by space not work in some space string


2   2019-11-21 21:51:24 0   1
1   2019-11-25 13:32:06 0   1
1   2019-11-25 13:36:31 0   1
1234    2019-11-25 13:38:55 0   1

I have output data from website like above, then i want to explode that data to array ouput like this, Array ( [0] => 2 [1] => 2019-11-21 [2] => 21:51:24 [3] => 0 [4] => 1 1 [5] => 2019-11-25 [6] => 13:32:06 [7] => 0 [8] => 1 1 [9] => 2019-11-25 [10] => 13:36:31 [11] => 0 [12] => 1 1234 [13] => 2019-11-25 [14] => 13:38:55 [15] => 0 [16] => 1 );

but when i try to run the output like

Array
(
    [0] => 2    2019-11-21
    [1] => 21:51:24 0   1
    [2] => 
    [3] => 1    2019-11-25
    [4] => 13:32:06 0   1
    [5] => 
    [6] => 1    2019-11-25
    [7] => 13:36:31 0   1
    [8] => 
    [9] => 1234 2019-11-25
    [10] => 13:38:55    0   1
)

why some space not in explode?


Solution

  • At first you need to replace multiple space with single space then perform explode on to the result

    $string = '2   2019-11-21 21:51:24 0   1';
    $singleSpace = preg_replace('!\s+!', ' ', $string);
    $explodeData = explode(" ",$singleSpace);