Reputation: 417
How do I use preg_match in php to match a whole word on its own and not when it's part of another word.
ie.
151 : 151-220 - should be a match. 51 : 151-220 - should not be a match.
Help!
Upvotes: 0
Views: 277
Reputation: 360572
Use the \b
modifier, which indicates word boundaries.
preg_match('/\b...\b/', $your_string);
Upvotes: 0
Reputation: 23216
Try using \b
in your regular expressions. They are word boundaries. E.g: /\b151\b/
.
Upvotes: 0
Reputation: 37803
preg_match('/\b151\b/', $string)
\b
matches a "word boundary"
Upvotes: 1