如何检查句子中是否存在某个单词

发布于 2024-12-14 19:32:38 字数 201 浏览 1 评论 0原文

例如,如果我的句子是 $sent = 'how are you'; 并且如果我使用 strstr($sent, $key) 它将返回 true 因为我的句子中有 ho

我正在寻找的是一种返回 true 的方法,如果我只搜索 how、are 或 you。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

时常饿 2024-12-21 19:32:38

您可以使用函数 preg-match,该函数使用 带有单词边界的正则表达式

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

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';
}
岛歌少女 2024-12-21 19:32:38

如果您想检查同一字符串中的多个单词,并且正在处理大字符串,那么这会更快:

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

然后您可以使用以下命令检查单词:

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

此方法速度快如闪电。

但要检查短字符串中的几个单词,请使用 preg_match。

更新:

如果您确实要使用它,我建议您像这样实现它以避免出现问题:

$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();

然后双空格、换行符、标点符号和大写字母不会产生漏报。

此方法在检查大字符串(即整个文本文档)中的多个单词时要快得多,但如果您只想查找正常大小的字符串中是否存在单个单词,则使用 preg_match 会更有效。

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.

春花秋月 2024-12-21 19:32:38

您可以做的一件事是将句子按空格分解成数组。

首先,您需要删除任何不需要的标点符号。
以下代码删除除字母、数字或空格之外的任何内容:

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

现在,您所拥有的只是由空格分隔的单词。创建一个按空格分割的数组...

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

最后,您可以进行检查。以下是所有步骤的组合。

// 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'

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'
彡翼 2024-12-21 19:32:38

@codaddict的答案在技术上是正确的,但如果您正在搜索的单词是由用户提供的,则您需要转义搜索单词中具有特殊正则表达式含义的任何字符。例如:

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

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

@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";
}
萌梦深 2024-12-21 19:32:38

认识到 Abhi 的答案后,提出了一些建议:

  1. 我在正则表达式中添加了 /i,因为句子单词可能被不区分大小写地处理
  2. 我根据记录的 preg_match 返回值在比较中添加了显式 === 1

    $needle = preg_quote($needle);
    返回 preg_match("/\b$needle\b/i", $haystack) === 1;
    

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;
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文