0

So currently I have a problem. I have this snippet of code to see if a phrase is present in another phrase:

if(strstr($matches[1], $query))

So for example if:

$matches[1] = "arctic white"
$query = "arctic"

In the case above, the code would detect that the phrase "arctic" is in the phrase "arctic white" although, what I want is for it to detect if it is inside words as well and not just phrases.

For example if:

$matches[1] = "antarctica"
$query = "arctic"

In that case the script would NOT detect the word "arctic" in "antarctica" although it is. So I was wondering, how can I edit if(strstr($matches[1], $query)) so it would detect ALL words that have the $query content in it? Please help!

S17514
  • 265
  • 2
  • 7
  • 16

2 Answers2

2

You can use preg_match() for much better result. The preg_match doesn't encompass regular expressions only. It can do exactly what you need. i.e.:

if (preg_match("/arctic/i", "antarctica")) {
    // it is there do something
} else {
    // it is not there do something else
}

btw, the small "i" means case sensitivity, check PHP manual for more examples: http://php.net/manual/en/function.preg-match.php

Milan
  • 3,209
  • 1
  • 35
  • 46
  • He corrected his post. He wants the words, you should show him how to collect matches. – oxygen Jun 09 '12 at 19:42
  • but he is showing just one array key -> value. Does he mean the key will hold a long text which needs to be searched ? Please clarify. – Milan Jun 16 '12 at 08:12
0

use strpos()

example:

$word = "antarctica";
$find = "arctic";

$i = strpos($word, $find);

if($i === false)
{
echo "not found";
}

else
{
echo "found";
}
PsychoMantis
  • 993
  • 2
  • 13
  • 29