Correct syntax for matching a string inside a variable against an array

Posted by Jamex on Stack Overflow See other posts from Stack Overflow or by Jamex
Published on 2010-05-30T18:20:46Z Indexed on 2010/05/30 18:22 UTC
Read the original article Hit count: 184

Filed under:
|
|
|
|

Hi,

I have a variable, $var, that contains a string of characters, this is a dynamic variable that contains the values from inputs.

$var could be 'abc', or $var could be 'blu',

I want to match the string inside variable against an array, and return all the matches.

$array = array("blue", "red", "green");

What is the correct syntax for writing the code in php, my rough code is below

$match = preg_grep($var, $array); (incorrect syntax of course)

I tried to put quotes and escape slashes, but so far no luck. Any suggestion?

TIA

© Stack Overflow or respective owner

Related posts about php

Related posts about regex