简体   繁体   中英

PHP Check if array element exists in any part of the string

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

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 ;)

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

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.

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>";                
    }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM