'PHP explode string into array with space delimiter when string has multiple spaces?

My PHP code below will explode a string into array. It allows to use multiple delimiters for detecting where the array values should be from the string.

In the demo one of the delimiter values is a space. The problem is if the string has more than 1 consecutive space, it will generate empty array values.

For example key key2 key3 key4 would generate array with:

Array
(
    [0] => key
    [1] => key2
    [2] => key3
    [3] => 
    [4] => key4
)

and the desired output is:

Array
(
    [0] => key
    [1] => key2
    [2] => key3
    [4] => key4
)

How can I fix this?


/**
 * Explode a string into array with multiple delimiters instead of the default 1 delimeter that explode() allows!
 * @param  array $delimiters - array(',', ' ', '|') would make a string with spaces, |, or commas turn into array
 * @param string $string  text with the delimeter values in it that will be truned into an array
 * @return array - array of items created from the string where each delimiter  in string made a new array key
 */
function multiexplode ($delimiters, $string) {
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $array = explode($delimiters[0], $ready);
    $array = array_map('trim', $array);
    return  $array;
}

$tagsString  = 'PHP JavaScript,WebDev Apollo   jhjhjh';
$tagsArray = multiexplode(array(',',' '), $tagsString);
print_r($tagsArray);

Output array

Array
(
    [0] => PHP
    [1] => JavaScript
    [2] => WebDev
    [3] => Apollo
    [4] => 
    [5] => 
    [6] => jhjhjh
)
php


Solution 1:[1]

You can solve your problem and simplify the code by using preg_split(). You can apply a regex including all delimiters in a character class with a quantifier and \s* to consume whitespaces around the delimiters.

Code:

$array = preg_split("/\s*[" . preg_quote(implode("", $delimiters), "/") . "]+\s*/", $str);
                      ?????????????????????????????????????????????????????????? 
      Consuming any whitespaces                   ?             Consuming any whitespaces 
      around delimiter                            ?             around delimiter
                                   ????????????????
                                   ? []           ? Character class with quantifier
                                   ? implode()    ? Converting array into a string
                                   ? preg_quote() ? Escaping special characters

Solution 2:[2]

You can also use :

array_filter($tagsArray);

Or use :

array_diff( $tagsArray, array( '' ));

Solution 3:[3]

There is already a working preg_split() answer, but maybe more flexible to just use PREG_SPLIT_NO_EMPTY:

$tagsArray = preg_split('/[ ,]/', $tagsString, null, PREG_SPLIT_NO_EMPTY);  // or '/ |,/'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 FareedMN
Solution 3