Using reg ex to search array??


#1

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.


#2

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.


#3

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