douraoyw194498
2014-08-10 14:02
浏览 130
已采纳

PHP检查字符串的任何部分是否存在数组元素

I know how to find if your string equals an array value:

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

$string = "white";

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

...but how do I find if the string CONTAINS any part of the array values?

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

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

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

图片转代码服务由CSDN问答提供 功能建议

我知道如何查找字符串是否等于数组值:

   $ colors = array(“blue”,“red”,“white”); 
 
 $ string =“white”; 
 
if(!in_array($ string,$ colors)){
  echo'not found'; 
} 
   
 
 

...但是如何找到字符串CONTAINS数组值的任何部分? \ n

  $ colors = array(“blue”,“red”,“white”); 
 
 $ string =“whitewash”;  //我希望在数组中找到它
 
if(!in_array($ string,$ colors)){
 echo'not found'; 
} 
   
  
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dscss8996 2014-08-10 14:09
    已采纳

    Just loop the array containing the values, and check if they are found in the input string, using 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"; 
    
        }
    }
    

    You can wrap it in a function:

    function findString($array, $string) {
    
        foreach ( $array as $a ) {
    
            if ( strpos ( $string , $a ) !== FALSE )
                 return true;
    
        }
    
        return false;
    } 
    
    var_dump( findString ( $colors , "whitewash" ) ); // TRUE
    
    已采纳该答案
    打赏 评论
  • dongzha0813 2014-08-10 14:07

    You would have to iterate over each array element and individually check if it contains it (or a substr of it).

    This is similar to what you want to do: php check if string contains a value in array

    打赏 评论
  • dsj0312 2014-08-10 14:09
    $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 will contain all $colors that have a match in $string.

    if(empty($hits)) {
        echo 'not found';
    }
    
    打赏 评论
  • douju1928 2014-08-10 14:12

    Or in one shot:

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

    This can also be expanded to only allow words that start with an item from your array:

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

    Or case-insensitive:

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

    CI with starting only:

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

    Or anything really ;)

    打赏 评论
  • douyong8801 2014-08-10 14:15

    There is no built-in function for that, but you could do something like:

    $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';
    }
    

    This basically makes a regex from your array and matches the string against it. Good method, unless your array is really large.

    打赏 评论
  • donglinxia1541 2014-08-10 14:35

    Try this working solution

    $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>";                
        }
    }
    
    打赏 评论

相关推荐 更多相似问题