开发者

PHP match multiple whole words in regex

开发者 https://www.devze.com 2023-03-03 12:24 出处:网络
I\'d like to match a 开发者_开发百科string for word1 or word but a string like \'this is a testword\' should NOT give a match. I\'m trying to do preg_match(\'/^\\b(word|word1)\\b/i\',$string) but no l

I'd like to match a 开发者_开发百科string for word1 or word but a string like 'this is a testword' should NOT give a match. I'm trying to do preg_match('/^\b(word|word1)\b/i',$string) but no luck


I think your problem is the ^ at the start:

preg_match('/\b(word|word1)\b/i',$string);

Note that the ^ denotes the beginning of the string, so you are looking for something that starts with the options you are giving.


Seems to work fine if you remove the ^ from the beginning of your RegEx ..

0

精彩评论

暂无评论...
验证码 换一张
取 消