4

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

6 Answers 6

4

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

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

2

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

Comments

1

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.

Comments

0

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

Comments

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

2 Comments

$hits will be empty
Did you test it , its empty .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.