How to check if a word exists in a sentence
Asked Answered
R

5

5

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?

Rebecarebecca answered 10/11, 2011 at 4:49 Comment(0)
S
6

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.

Skeet answered 10/11, 2011 at 4:57 Comment(1)
If you're going to use this, see my update to make it more reliable.Skeet
O
8

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';
}
Orv answered 10/11, 2011 at 4:50 Comment(0)
S
6

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.

Skeet answered 10/11, 2011 at 4:57 Comment(1)
If you're going to use this, see my update to make it more reliable.Skeet
P
3

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'
Prophecy answered 10/11, 2011 at 5:3 Comment(0)
C
1

@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";
}
Clericalism answered 10/11, 2011 at 4:55 Comment(2)
Ya, the searched word is provided by a user. Thank you for the insight.Rebecarebecca
Closing parenthesis is missing for the if.Ketcham
S
0

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;
    
Scraperboard answered 17/12, 2012 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.