Question

For example, if my sentence is $sent = 'how are you'; and if I search for $key = 'ho' using strstr($sent, $key) it will return true because my sentence has ho in it.

What I'm looking for is a way to return true if I only search for how, are or you. How can I do this?

Was it helpful?

Solution

If you want to check for multiple words in the same string, and you're dealing with large strings, then this is faster:

$text = explode(' ',$text);
$text = array_flip($text);

Then you can check for words with:

if (isset($text[$word])) doSomething();

This method is lightning fast.

But for checking for a couple of words in short strings then use preg_match.

UPDATE:

If you're actually going to use this I suggest you implement it like this to avoid problems:

$text = preg_replace('/[^a-z\s]/', '', strtolower($text));
$text = preg_split('/\s+/', $text, NULL, PREG_SPLIT_NO_EMPTY);
$text = array_flip($text);

$word = strtolower($word);
if (isset($text[$word])) doSomething();

Then double spaces, linebreaks, punctuation and capitals won't produce false negatives.

This method is much faster in checking for multiple words in large strings (i.e. entire documents of text), but it is more efficient to use preg_match if all you want to do is find if a single word exists in a normal size string.

OTHER TIPS

You can use the function preg-match that uses a regex with word boundaries:

if(preg_match('/\byou\b/', $input)) {
  echo $input.' has the word you';
}

One thing you can do is breaking up your sentence by spaces into an array.

Firstly, you would need to remove any unwanted punctuation marks. The following code removes anything that isn't a letter, number, or space:

$sent = preg_replace("/[^a-zA-Z 0-9]+/", " ", $sent);

Now, all you have are the words, separated by spaces. To create an array that splits by space...

$sent_split = explode(" ", $sent);

Finally, you can do your check. Here are all the steps combined.

// The information you give
$sent = 'how are you';
$key  = 'ho';

// Isolate only words and spaces
$sent = preg_replace("/[^a-zA-Z 0-9]+/", " ", $sent);
$sent_split = explode(" ", $sent);

// Do the check
if (in_array($key, $sent))
{
    echo "Word found";
}
else
{
    echo "Word not found";
}

// Outputs: Word not found
//  because 'ho' isn't a word in 'how are you'

@codaddict's answer is technically correct but if the word you are searching for is provided by the user, you need to escape any characters with special regular expression meaning in the search word. For example:

$searchWord = $_GET['search'];
$searchWord = preg_quote($searchWord);

if (preg_match("/\b$searchWord\b", $input) {
  echo "$input has the word $searchWord";
}

With recognition to Abhi's answer, a couple of suggestions:

  1. I added /i to the regex since sentence-words are probably treated case-insensitively
  2. I added explicit === 1 to the comparison based on the documented preg_match return values

    $needle = preg_quote($needle);
    return preg_match("/\b$needle\b/i", $haystack) === 1;
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top