【问题标题】:PHP Check if array element exists in any part of the stringPHP检查字符串的任何部分是否存在数组元素
【发布时间】:2014-08-10 14:02:57
【问题描述】:

我知道如何判断你的字符串是否等于数组值:

$colors = array("blue","red","white");

$string = "white";

if (!in_array($string, $colors)) {
    echo 'not found';
}

...但是如何找到字符串是否包含数组值的任何部分?

$colors = array("blue","red","white");

$string = "whitewash"; // I want this to be found in the array

if (!in_array($string, $colors)) {
    echo 'not found';
}

【问题讨论】:

    标签: php


    【解决方案1】:

    或者一口气:

    if( preg_match("(".implode("|",array_map("preg_quote",$colors)).")",$string,$m)) {
        echo "Found ".$m[0]."!";
    }
    

    这也可以扩展为只允许以数组中的项目开头的单词:

    if( preg_match("(\b(?:".implode("|",array_map("preg_quote",$colors))."))",$string,$m)) {
    

    或不区分大小写:

    if( preg_match("(".implode("|",array_map("preg_quote",$colors)).")i",$string,$m)) {
    

    仅开始的 CI:

    if( preg_match("(\b(?:".implode("|",array_map("preg_quote",$colors))."))i",$string,$m)) {
    

    或者其他任何东西;)

    【讨论】:

      【解决方案2】:

      只需循环包含值的数组,并检查是否在输入字符串中找到它们,使用 strpos

      $colors = array("blue","red","white");
      
      $string = "whitewash"; // I want this to be found in the array
      
      foreach ( $colors as $c ) {
      
          if ( strpos ( $string , $c ) !== FALSE ) {
      
               echo "found"; 
      
          }
      }
      

      你可以把它包装在一个函数中:

      function findString($array, $string) {
      
          foreach ( $array as $a ) {
      
              if ( strpos ( $string , $a ) !== FALSE )
                   return true;
      
          }
      
          return false;
      } 
      
      var_dump( findString ( $colors , "whitewash" ) ); // TRUE
      

      【讨论】:

        【解决方案3】:

        试试这个可行的解决方案

        $colors = array("blue", "red", "white");
        $string = "whitewash";       
        foreach ($colors as $color) {
            $pos = strpos($string, $color);
            if ($pos === false) {
               echo "The string '$string' not having substring '$color'.<br>";      
            } else {
                 echo "The string '$string'  having substring '$color'.<br>";                
            }
        }
        

        【讨论】:

          【解决方案4】:

          没有内置函数,但您可以执行以下操作:

          $colors = array("blue","red","white");
          
          $string = "whitewash"; // I want this to be found in the array
          
          if (!preg_match('/\Q'.implode('\E|\Q',$colors).'\E/',$string)) {
              echo 'not found';
          }
          

          这基本上是从您的数组中生成一个正则表达式并将字符串与之匹配。好方法,除非你的数组真的很大。

          【讨论】:

            【解决方案5】:

            您必须遍历每个数组元素并单独检查它是否包含它(或它的子字符串)。

            这与您想要做的类似: php check if string contains a value in array

            【讨论】:

              【解决方案6】:
              $colors = array("blue","red","white");
              
              $string = "whitewash"; // I want this to be found in the array
              
              $hits = array();
              foreach($colors as $color) {
                 if(strpos($string, $color) !== false) {
                    $hits[] = $color;
                 }
              }
              

              $hits 将包含在 $string 中匹配的所有 $colors。

              if(empty($hits)) {
                  echo 'not found';
              }
              

              【讨论】:

              • $hits 将为空
              • 你测试了吗,它是空的。
              猜你喜欢
              • 1970-01-01
              • 2020-11-12
              • 1970-01-01
              • 2011-12-11
              • 1970-01-01
              • 2013-09-27
              • 2011-12-23
              • 2013-10-31
              • 2016-09-22
              相关资源
              最近更新 更多