php split on each whitespace, but not after specific word to create array from string

49 Views Asked by At

Take this string:

$string = "wanted NOT IN OR"

preg_match_all('/\w+/', $string, $WordArray);
//  outputs each word into array

But what if I do not want to split on the whitespace right after the word "NOT".
Keeping the word "NOT" and the following word together, and then keep splitting as usual.

The array should end up looking like this:

$WordArray = [
    'wanted',
    'NOT IN',
    'OR'
'];

Did try with something like this, but.. yeah. That did not split as expected...

$WordArray = preg_split('/\s|\bNOT\s/');
0

There are 0 best solutions below