I need a regex to find matches of words in a string regardless of case, but not larger words where the target word is a part of.
For instance, if the target word is "apple", the regex should find it in the following strings:
"I found an apple."
"Apple, it's on the ground"
"That ApPLE is nice"
Warning: preg_match(): Delimiter must not be alphanumeric or backslash
You need to add
/ to delimit the regex. So a solution could look like this:
preg_match_all('/\bapple\b/i', $string, $matches); $count = count($matches); // group 0 are the full matches
i modifier matches case-insensitive and instead of the
g modifier we have to use