-1

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.

smartmouse
  • 13,912
  • 34
  • 100
  • 166

3 Answers3

2

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('/ +/',...).

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • It works, but how to edit it if i encounter strings that contains `word1!word2`. In this case it doesn't split. – smartmouse Jun 10 '15 at 16:00
  • Just edited my reply: use a group consisting of a single character belonging to the character class `[&!]` instead of the single character `&` and replace it with its own occurence (`$1`) . – Carsten Massmann Jun 10 '15 at 16:04
  • The reason for what i asked this it's because i'm trying to parse users search strings. Maybe you can help me with this: http://stackoverflow.com/questions/30760625/how-to-parse-user-search-string-for-postgresql-query – smartmouse Jun 10 '15 at 16:21
1

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
)
kamal pal
  • 4,187
  • 5
  • 25
  • 40
0

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',
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136