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.
例如,如果我的句子是$ sent ='你好吗';如果我使用strstr($ sent,$ key)搜索$ key ='ho',它将返回true,因为我的句子中有ho。
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?
我正在寻找的是一种方法,如果我只搜索你是怎么回事,你或者是你。我怎样才能做到这一点?
5 个解决方案
#1
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.
但是为了检查短字符串中的几个单词,请使用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.
这种方法在检查大字符串中的多个单词(即整个文本文档)时要快得多,但如果你想要做的就是查找正常大小字符串中是否存在单个单词,那么使用preg_match会更有效。
#2
6
You can use the function preg-match
that uses a regex with word boundaries:
您可以使用使用带有字边界的正则表达式的函数preg-match:
if(preg_match('/\byou\b/', $input)) {
echo $input.' has the word you';
}
#3
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'
#4
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:
@ codaddict的答案在技术上是正确的,但如果您要搜索的单词是由用户提供的,则您需要在搜索词中转义任何具有特殊正则表达式的字符。例如:
$searchWord = $_GET['search'];
$searchWord = preg_quote($searchWord);
if (preg_match("/\b$searchWord\b", $input) {
echo "$input has the word $searchWord";
}
#5
0
With recognition to Abhi's answer, a couple of suggestions:
承认Abhi的回答,提出了几点建议:
- I added /i to the regex since sentence-words are probably treated case-insensitively
- 我将/ i添加到正则表达式,因为句子单词可能不区分大小写
-
I added explicit === 1 to the comparison based on the documented preg_match return values
我根据记录的preg_match返回值在比较中添加了显式=== 1
$needle = preg_quote($needle); return preg_match("/\b$needle\b/i", $haystack) === 1;
#1
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.
但是为了检查短字符串中的几个单词,请使用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.
这种方法在检查大字符串中的多个单词(即整个文本文档)时要快得多,但如果你想要做的就是查找正常大小字符串中是否存在单个单词,那么使用preg_match会更有效。
#2
6
You can use the function preg-match
that uses a regex with word boundaries:
您可以使用使用带有字边界的正则表达式的函数preg-match:
if(preg_match('/\byou\b/', $input)) {
echo $input.' has the word you';
}
#3
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'
#4
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:
@ codaddict的答案在技术上是正确的,但如果您要搜索的单词是由用户提供的,则您需要在搜索词中转义任何具有特殊正则表达式的字符。例如:
$searchWord = $_GET['search'];
$searchWord = preg_quote($searchWord);
if (preg_match("/\b$searchWord\b", $input) {
echo "$input has the word $searchWord";
}
#5
0
With recognition to Abhi's answer, a couple of suggestions:
承认Abhi的回答,提出了几点建议:
- I added /i to the regex since sentence-words are probably treated case-insensitively
- 我将/ i添加到正则表达式,因为句子单词可能不区分大小写
-
I added explicit === 1 to the comparison based on the documented preg_match return values
我根据记录的preg_match返回值在比较中添加了显式=== 1
$needle = preg_quote($needle); return preg_match("/\b$needle\b/i", $haystack) === 1;