How to split string on multiple delimiters keeping some delimiters?

475 views Asked by At

i'm looking for a way to split a string in several words, based on some delimiters.

For example the string word1&word2 !word3 word4 &word5 should give me an array with the following words:

word1
&word2
!word3
word4
&word5

How to reach that? I tried several solution with str_replace() but i cannot figure out the best way to obtain what i need. Maybe the solution could be using regular expressions, but i do not know how to use them.

3

There are 3 answers

3
Carsten Massmann On BEST ANSWER

Try this:

$src='word1&word2 !word3 word4 &word5';
$arr=explode(' ',$src=preg_replace('/(?<=[\w])([&!])/',' $1',$src));
echo join('<br>',$arr); // present the result ...

First change any occurence of a group consisting of a single character of class [&!] that is preceded by a 'word'-character into ' $1' (=itself, preceded with a blank) and then explode()the string using the blanks as separators.

If you need to deal with multiple blanks as separators between the words you could of course replace the (faster) explode(' ',...) with a slighty more "refined" preg_split('/ +/',...).

0
kamal pal On

You can do use of preg_split, and do some customization as needed. see example below:-

function customExplode($string){
    if($matches = preg_split('/[\s&!]+/i', $string, null, PREG_SPLIT_OFFSET_CAPTURE)){
        $return = array();
        foreach ($matches as $match) {
            $return[] = (($match[1]-1) >= 0) ? substr($string, $match[1]-1, 1).$match[0] : $match[0];
        }
        return $return;
    } else {
        return $string;
    }
}
$word = 'word1&word2 !word3 word4 &word5';
print_r(customExplode($word));

Results

Array
(
    [0] => word1
    [1] => &word2
    [2] => !word3
    [3] =>  word4
    [4] => &word5
)
0
mickmackusa On

Previous answers have overcomplicated this task.

The required coding/pattern logic is to split on each space OR on the position before & or !.

Code #1: (Demo) - split on space or position between non-space and a & or !

var_export(
    preg_split('/ |(?<! )(?=[&!])/', $string)
);

Code #2: (Demo) - hastily split on spaces or positions before & or ! and omit empty elements from result

var_export(
    preg_split('/ |(?=[&!])/', $string, 0, PREG_SPLIT_NO_EMPTY)
);

Both Output:

array (
  0 => 'word1',
  1 => '&word2',
  2 => '!word3',
  3 => 'word4',
  4 => '&word5',
)