Using reg ex to search array??

Hi all

Can you search an array via a regular expression? I tried the following but couldn’t get it to return anything. It should return the key of the matching array value:

$array = array(“homer”, “j”, “simpson”);
echo $array[array_search(’.’, $array)]; //return initial

It’s possible my syntax for the reg ex is wrong, but I read somewhere that ‘.’ will look for a single character of any type.

Thanks in advance.

Not directly. You can probably use something like array_filter() or one of those callback-based functions or use a loop to do the job.

Alright cheers. Seems a shame, as is often the case, that there aren’t short cuts for this sort of thing.

Sponsor our Newsletter | Privacy Policy | Terms of Service