PHP preg_match - 匹配一个完全独立的关键字?

How can I match a keyword that is completely on its own?

For instance I only want to match hello in a string like these,

/say/hello/world/
/say/hello/

but not in these,

/say/helloworld/
/say/hello-world/
/say/hello+world/
/say_hello_world/

So far with this pattern,

var_dump(preg_match('~.*?(?=hello)~i', $string)); // int 1

It matches all of them!

I only want /something/hello/world/ to be int 1 - is it possible?

Change the lookahead to include the slashes as well:

~.*?(?=/hello/)~i

Code:

var_dump(preg_match('~.*?(?=/hello/)~i', $string));

You can use this regex with lookarounds:

(?<=/)hello(?=/)

RegEx Demo

Code:

if ( preg_match_all('~(?<=/)hello(?=/)~i', $string, $m) )
   print_r($m);