0

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/');
7
  • What are the possible values of $string? Commented Feb 16, 2023 at 11:22
  • could be a lot. But the first word would always be a database fieldname (which could be just about any word. Next could be IN | BETWEEN | LIKE | AND | OR, and sometimes with NOT i front of one or more of these words... Commented Feb 16, 2023 at 11:33
  • 1
    How about this regex /NOT\s\w+|\w+/? Commented Feb 16, 2023 at 11:51
  • So something like this? 3v4l.org/4Q6fi Commented Feb 16, 2023 at 12:47
  • Yes. The first regex seems to do the trick. Thanks. I couldn't figure out how to use the | part in this.. Commented Feb 16, 2023 at 14:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.