查找字符串中的单词

发布于 2024-11-28 15:26:18 字数 304 浏览 0 评论 0原文

任何人都可以建议我如何执行此操作:假设我有字符串 $text 来保存用户输入的文本。我想使用“if 语句”来查找字符串是否包含单词 $word1 $word2$word3 之一。如果没有,请允许我运行一些代码。

if ( strpos($string, '@word1' OR '@word2' OR '@word3') == false ) {
    // Do things here.
}

我需要这样的东西。

Can anyone suggest how I would do this: Say if I had the string $text which holds text entered by a user. I want to use an 'if statement' to find if the string contains one of the words $word1 $word2 or $word3 . Then if it doesn't, allow me to run some code.

if ( strpos($string, '@word1' OR '@word2' OR '@word3') == false ) {
    // Do things here.
}

I need something like that.

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

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

发布评论

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

评论(6

2024-12-05 15:26:19

你可以使用 preg_match,像这样

if (preg_match("/($word1)|($word2)|($word3)/", $string) === 0) {
       //do something
}

you can use preg_match, like this

if (preg_match("/($word1)|($word2)|($word3)/", $string) === 0) {
       //do something
}
戒ㄋ 2024-12-05 15:26:19

最好使用 stripos 而不是 strpos,因为它不区分大小写。

It may be better to use stripos instead of strpos because it is case-insensitive.

游魂 2024-12-05 15:26:18

更灵活的方法是使用单词数组:

$text = "Some text that containts word1";    
$words = array("word1", "word2", "word3");

$exists = false;
foreach($words as $word) {
    if(strpos($text, $word) !== false) {
        $exists = true;
        break;
    }
}

if($exists) {
    echo $word ." exists in text";
} else {
    echo $word ." not exists in text";
}

结果是:word1存在于文本中

More flexibile way is to use array of words:

$text = "Some text that containts word1";    
$words = array("word1", "word2", "word3");

$exists = false;
foreach($words as $word) {
    if(strpos($text, $word) !== false) {
        $exists = true;
        break;
    }
}

if($exists) {
    echo $word ." exists in text";
} else {
    echo $word ." not exists in text";
}

Result is: word1 exists in text

墨小墨 2024-12-05 15:26:18
if ( strpos($string, $word1) === false && strpos($string, $word2) === false && strpos($string, $word3) === false) {

}
if ( strpos($string, $word1) === false && strpos($string, $word2) === false && strpos($string, $word3) === false) {

}
丑疤怪 2024-12-05 15:26:18

定义以下函数:

function check_sentence($str) {
  $words = array('word1','word2','word3');

  foreach($words as $word)
  {
    if(strpos($str, $word) > 0) {
    return true;
    }
  }

  return false;
}

并像这样调用它:

if(!check_sentence("what does word1 mean?"))
{
  //do your stuff
}

Define the following function:

function check_sentence($str) {
  $words = array('word1','word2','word3');

  foreach($words as $word)
  {
    if(strpos($str, $word) > 0) {
    return true;
    }
  }

  return false;
}

And invoke it like this:

if(!check_sentence("what does word1 mean?"))
{
  //do your stuff
}
赠佳期 2024-12-05 15:26:18

正如我的之前的回答

if ($string === str_replace(array('@word1', '@word2', '@word3'), '', $string))
{
   ...
}

As in my previous answer:

if ($string === str_replace(array('@word1', '@word2', '@word3'), '', $string))
{
   ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文