The Wolf
The Wolf

Reputation: 195

using strpos to return true if a string contains to any of the strings from a set

The only function I can use is strpos, say I have set of strings: cat dog elephant

and I want to know if a keyword contains either to any of the strings above.

for at => True (contains inside cat) for og => True (contains inside dog) for asdasd => False (contains inside elephant)

I tried this:

strpos("keyword",("cat" || "dog" || "elephant"))

but no luck.

Hope somebody can help

Upvotes: 0

Views: 88

Answers (1)

KIKO Software
KIKO Software

Reputation: 16688

Are there any restrictions on your keyword and strings? If they will never contain a certain character, say '~', then you can simply use this:

if (strpos(<your keyword>,implode('~',<array of strings>)) !== FALSE)
{
  <do something>
}

This cannot be used when you have no control over the characters in your keyword and strings. In that can you can check each string in the array seperately:

foreach (<array of strings> as $string)
{
  if (strpos(<your keyword>,$string) !== FALSE)
  {
     <do something>
  }
}

An array is clearly the way to go to store your strings.

Upvotes: 1

Related Questions