I cannot seem to be able to find a way to not return a match if a string exists but **not immediately** before another string.

I am able to not return a match if a string exists **immediately** before another string, with the following.

```
$string = 'Stackoverflow hello world foobar test php';

$regex = "~(Stackoverflow).*?(?<!(test\s))(php)~i";

if(preg_match_all($regex,$string,$match))
    print_r($match);

```

In this example, we want to return a match if we have the word *Stackoverflow* and *php* but only if the word *test*(with a space character) does not exist before the word *php*.

This doesn't return any result which is good.

Lets now say I want to match *php* but only if the word *foobar* doesn't exist somewhere between *Stackoverflow* and *php*, I assumed I could do the following.

```
$string = 'Stackoverflow hello world foobar test php';

$regex = "~(Stackoverflow).*?(?<!(foobar)).*?(php)~i";

if(preg_match_all($regex,$string,$match))
    print_r($match);

```

(I have changed the negative look behind string to (foobar), and added .\*? after)

I would also like to say that I cannot always know what words will exist between *foobar* and *php*, sometimes there will be none, sometimes 200, but I do have some positioning information (after *Stackoverflow* and before *php*).