TheBounder
TheBounder

Reputation: 417

preg_match in php

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

Answers (3)

Marc B
Marc B

Reputation: 360572

Use the \b modifier, which indicates word boundaries.

preg_match('/\b...\b/', $your_string);

Upvotes: 0

Sander Marechal
Sander Marechal

Reputation: 23216

Try using \b in your regular expressions. They are word boundaries. E.g: /\b151\b/.

Upvotes: 0

VoteyDisciple
VoteyDisciple

Reputation: 37803

preg_match('/\b151\b/', $string)

\b matches a "word boundary"

Upvotes: 1

Related Questions