【问题标题】:How can I check if a word is contained in another string using PHP?如何使用 PHP 检查一个单词是否包含在另一个字符串中?
【发布时间】:2010-11-04 09:16:10
【问题描述】:

伪代码

text = "I go to school";
word = "to"
if ( word.exist(text) ) {
    return true ;
else {
    return false ;
}

我正在寻找一个 PHP 函数,如果该单词存在于文本中则返回 true。

【问题讨论】:

标签: php string


【解决方案1】:

您有几个选项,具体取决于您的需要。对于这个简单的示例,strpos() 可能是使用起来最简单、最直接的函数。如果您需要对结果做一些事情,您可能更喜欢strstr()preg_match()。如果您需要使用复杂的图案而不是绳子作为针,您需要preg_match()

$needle = "to";
$haystack = "I go to school";

strpos() and stripos() 方法(stripos() 不区分大小写):

if (strpos($haystack, $needle) !== false) echo "Found!";

strstr() and stristr() method(stristr 不区分大小写):

if (strstr($haystack, $needle)) echo "Found!";

preg_match method(正则表达式,更灵活但运行速度较慢):

if (preg_match("/to/", $haystack)) echo "Found!";

因为您要求一个完整的函数,所以您将它放在一起(针和干草堆的默认值):

function match_my_string($needle = 'to', $haystack = 'I go to school') {
  if (strpos($haystack, $needle) !== false) return true;
  else return false;
}

PHP 8.0.0 现在包含一个 str_contains 函数,其工作原理如下:

if (str_contains($haystack, $needle)) {
    echo "Found";
}

【讨论】:

  • 非 php'ers(如我自己)请注意 !== false 中的双等号,因为如果在字符串的开头(字符位置为零)找到匹配项,strpos() 可能会返回 0。
  • 你不能只做return (strpos($haystack, $needle) !== false);吗?结果完全相同,只是代码更简洁
【解决方案2】:
function hasWord($word, $txt) {
    $patt = "/(?:^|[^a-zA-Z])" . preg_quote($word, '/') . "(?:$|[^a-zA-Z])/i";
    return preg_match($patt, $txt);
}

如果 $word 是“to”,这将匹配:

  • “听我说”
  • “去月球”
  • “最新”

但不是:

  • “在一起”
  • “进入太空”

【讨论】:

  • 完美的解决方案,当然根据所提出的问题,接受的答案是正确的
  • 完美解决方案,因为 strpos() 即使匹配字符串的一部分而不是整个单词也会返回 true。示例:$haystack="检查那个叉子"; $needle="for";现在在这种情况下,strpos() 将找到匹配项并返回 true。但是 peg_match() 不会在大海捞针中找到 'for' 词,它会返回 0。
【解决方案3】:

使用:

return (strpos($text,$word) !== false); //case-sensitive

return (stripos($text,$word) !== false); //case-insensitive

【讨论】:

  • 你倒退了。 stripos 不区分大小写。
【解决方案4】:

strpos

<?php
$text = "I go to school";
$word = "to"
$pos = strpos($text, $word);

if ($pos === false) {
    return false;
} else {
    return true;
}
?>

【讨论】:

    【解决方案5】:
    $text="I go to school";
    return (strpos($text, 'to')!== false);
    

    The manual page you need to find the correct usage of strpos

    【讨论】:

      【解决方案6】:

      另一种方法(除了已经给出的 strpos 示例之外,是使用 'strstr' 函数:

      if (strstr($haystack, $needle)) {
         return true;
      } else {
         return false;
      }
      

      【讨论】:

        【解决方案7】:

        你可以使用这些字符串函数,

        strstr — 查找第一次出现的字符串

        stristr — 不区分大小写的 strstr()

        strrchr — 查找字符串中最后一次出现的字符

        strpos — 查找字符串中子字符串第一次出现的位置

        strpbrk — 在字符串中搜索任意一组字符

        如果这没有帮助,那么您应该使用preg 正则表达式

        preg_match — 执行正则表达式匹配

        【讨论】:

          【解决方案8】:

          PHP 8 新函数 str_contains

          if (str_contains('Foo Bar Baz', 'Foo')) {
            echo 'Found';
          }
          

          在php中使用strpos函数。

          $text = "I go to school";
          $word = "to"
          if (strpos($text,$word) !== false ) {
              echo 'true';
          }
          

          【讨论】:

            【解决方案9】:

            @mrclay

            ​​>

            不能'我们只是这样做

            "/(?:^|\w+)" . preg_quote($word, '/') . "(?:$|\w+)/i"
            

            以便它检查开始或空白,以及结束或空白。

            【讨论】:

              猜你喜欢
              • 2021-05-06
              • 2012-05-20
              • 2013-03-13
              • 2014-07-24
              • 2014-04-21
              • 1970-01-01
              • 2014-04-14
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多