$status == true is incorrect. If you find position at 0, that is a valid offset but will fail to pass a true check. You need to check for !== false (such as my example with "one" where it finds no results in the above string).
With an array you could easily implode them as well for the pattern:
$find = array('seven', 'five', 'ten'); $string = "one two three four five"; array_map($find, 'preg_quote'); $sPattern = '/\b(?:' . implode('|', $find) . ')\b/'; preg_match_all($sPattern, $string, $matches, PREG_OFFSET_CAPTURE);
Then iterate the $matches. They'll be in offset 0, and are an array containing the offsets and the string (if you need the offsets for whatever reason). If you don't need the offsets, drop the modifier and simply iterate the $matches.
This would be better since you don't need to worry about adding spaces and matching what you don't want to match. If you were looking for "fifty", it would also match "fiftyfive" while the pattern would not. This way you also don't need to concern yourself with order.
The pattern matching does take a lot longer though, but the returns of the strpos will degrade as the find counts increase.
I like Tango's suggestion as well. That will eliminate the issue with finding things like "fifty" and matching "fiftyfive" for example.
Assuming of course you don't actually care about the offset, in which case I'd still suggest using preg.
I think its just the understanding that has changed
These approaches have different purposes.
Using in_array or an intersection of arrays looks for the same items, not containment items. It is also the fastest of the three since you are comparing individual tokens to other individual tokens.
Using strpos will find partials. It takes longer than an array check since you need to compare the entire string each time. It cannot find multiples.
Using preg has the greatest flexibility in what you can do. You can choose to either make partials or exact matches. It is the slowest of the three, but can also find duplicates (the in_array can as well).
If your goal is to check if a string contains any of the items and can match partials, without caring of which it matches, simply write a function that returns a boolean:
That would return true so long as any one of the array items match, but you cannot tell which one it was nor whether it was a partial. Likewise, if you want to use identical, you would use in_array.
The preg wouldn't require a function to do this since it returns all the matches as an array. No matches = no items in array.
I had this working since the 4th post like shown below
can you give feedback as to why this is not a good way?
It's not that it isn't a good way, there is nothing wrong with what you're doing. All I'm saying is that I would have use in_array() - a php function (that will no doubt be slightly quicker and more efficient) to check if each word is in the array.
As I said, nothing wrong with what you've done but there are always several ways of doing the same thing