regex.
Also, using if ($status == true) is incorrect. You should be using if ($status !== false) instead, consider searching for one:
PHP Code:
$string = "one two three four five";
$find = "one";
//$find2 = "three";
$status = strpos($string, $find);
if ($status == true) echo "Found";
else "Not Found";
Would produce no results (I presume you intend to actually print the not found, but in this one its just a string sitting by its lonesome ).
of course just forgot the echo thats all, as for that if ($status == true) echo "Found";
thats been working correctly for years, my question was how do I check for another
word as well
$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).
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
With an array you could easily implode them as well for the pattern:
PHP Code:
$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[0].
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.
Edit:
I like Tango's suggestion as well. That will eliminate the issue with finding things like "fifty" and matching "fiftyfive" for example.
Edit:
Assuming of course you don't actually care about the offset, in which case I'd still suggest using preg.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
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.
Please take another look at the first post, nothing has changed
It's still a string, that needs to be checked for two words "efficiently".
and with minimal code as always.
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 been working perfect so far.
PHP Code:
$find = array('seven', 'five', 'ten');
$string = "one two three four five";
foreach ($find as $n) {
if (strpos($string, $n) !== false)
echo "Found";}
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
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.