【问题标题】:How to check if a string contains a specific text [duplicate]如何检查字符串是否包含特定文本[重复]
【发布时间】:2013-02-24 15:55:26
【问题描述】:
<?php
$a = '';

if($a exist 'some text')
    echo 'text';
?>

假设我有上面的代码,语句“if($a exist 'some text')”怎么写?

【问题讨论】:

  • 你的意思是:if($a == 'some text')。这里有更多关于运营商的信息:php.net/manual/en/language.operators.comparison.php
  • 如果字符串的大小大于 0 ,则字符串中有一些文本。
  • 如果您正在检查字符串是否有任何文本,那么这应该可以工作 if(strlen($a) &gt; 0) echo 'text'; 或者如果您关心的是检查特定单词,请遵循 @Dai 答案。

标签: php


【解决方案1】:

使用strpos函数:http://php.net/manual/en/function.strpos.php

$haystack = "foo bar baz";
$needle   = "bar";

if( strpos( $haystack, $needle ) !== false) {
    echo "\"bar\" exists in the haystack variable";
}

在你的情况下:

if( strpos( $a, 'some text' ) !== false ) echo 'text';

请注意,我使用!== 运算符(而不是!= false== true 甚至只是if( strpos( ... ) ) {)是因为PHP 处理strpos 返回值的"truthy"/"falsy" 本质。

从 PHP 8.0.0 开始,您现在可以使用 str_contains

<?php
    if (str_contains('abc', '')) {
        echo "Checking the existence of the empty string will always 
        return true";
    }

【讨论】:

  • 不匹配不会返回 0 吗?
  • false &gt;= 0。你必须写!== false,为0 == false
  • @Blender 对不起,你是对的。我在考虑 .NET String.IndexOf,它在不匹配的情况下返回 -1。我已经更正了我的答案。
  • stripos() 用于 insensitive 字符串比较 ...
  • @PadronizaçãoSA ! 运算符将影响来自 strpos 的返回值的虚假性,这意味着 === 不会按预期方式工作。
【解决方案2】:

是否意味着检查 $a 是否为非空字符串? 所以它只包含任何文本? 然后以下将起作用。

如果 $a 包含一个字符串,您可以使用以下内容:

if (!empty($a)) {      // Means: if not empty
    ...
}

如果您还需要确认 $a 实际上是一个字符串,请使用:

if (is_string($a) && !empty($a)) {      // Means: if $a is a string and not empty
    ...
}

【讨论】:

  • 什么废话?谁问了所有这些事情?
【解决方案3】:

如果您需要知道字符串中是否存在某个单词,您可以使用它。由于您的问题尚不清楚您是否只想知道变量是否为字符串。其中 'word' 是您在字符串中搜索的单词。

if (strpos($a,'word') !== false) {
echo 'true';
}

或使用 is_string 方法。 whichs 在给定变量上返回 true 或 false。

<?php
$a = '';
is_string($a);
?>

【讨论】:

    【解决方案4】:

    您可以使用此代码

    $a = '';
    
    if(!empty($a))
      echo 'text';
    

    【讨论】:

      【解决方案5】:

      您可以使用strpos()stripos() 来检查字符串是否包含给定的针。它将返回找到它的位置,否则将返回 FALSE。

      在 PHP 中使用运算符 === 或 `!== 来区分 FALSE 和 0。

      【讨论】:

        【解决方案6】:

        您可以使用==comparison operator来检查变量是否等于文本:

        if( $a == 'some text') {
            ...
        

        您也可以使用strpos 函数返回第一次出现的字符串:

        <?php
        $mystring = 'abc';
        $findme   = 'a';
        $pos = strpos($mystring, $findme);
        
        // Note our use of ===.  Simply == would not work as expected
        // because the position of 'a' was the 0th (first) character.
        if ($pos === false) {
            echo "The string '$findme' was not found in the string '$mystring'";
        } else {
            echo "The string '$findme' was found in the string '$mystring'";
            echo " and exists at position $pos";
        }
        

        See documentation

        【讨论】:

          【解决方案7】:

          http://php.net/manual/en/function.strpos.php我想你很奇怪字符串中是否存在“某些文本”,对吧?

          if(strpos( $a , 'some text' ) !== false)
          

          【讨论】:

            【解决方案8】:

            空字符串是假的,所以你可以写:

            if ($a) {
                echo 'text';
            }
            

            尽管如果您询问该字符串中是否存在特定子字符串,您可以使用strpos() 来做到这一点:

            if (strpos($a, 'some text') !== false) {
                echo 'text';
            }
            

            【讨论】:

            • 如果你想让它找到“Some Text”、“SOME TEXT”等,请使用stripos(不区分大小写)
            猜你喜欢
            • 2019-08-15
            • 1970-01-01
            • 2014-01-08
            • 1970-01-01
            • 2015-06-24
            • 2012-04-21
            • 2021-12-05
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多