Edit: I meant to ask what is the best/fastest way to get the first match or the first xx matches.
I have an array
$arr = ('abc', 'ded', 'kjld', 'abr', 'cdfd');
I want to shuffle
this array first, and then retrieve ONLY the first value that matches the pattern /ab/
. So, the returned value could be abc or abr.
I looked at preg_grep
, but it will return an array of all the matches. Of course I could just retrieve the first value of the resultant array, but that is wasteful and requires an extra step of array manipulation. Is there another function or a preg_grep
switch that specifies return only the first matched value (or first 5 matched values). I have looked at preg_match
and preg_search
, but they don't seem to give what I want.