-1

How to apply preg_match on each element of an array

$array = [abc,def,ghi];

Right now i am doing this

foreach($array as $one_element){   
    if(!preg_match("/^[a-zA-Z0-9_. -]{1,23}$/",$one_element)){
        die("One of element  name is not valid");
    }
}

Is there any easier and faster way to do this ?

Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
beginner
  • 2,366
  • 4
  • 29
  • 53

1 Answers1

-1

Yes, with array_map:

array_map(
    function($elem) {
        if (!preg_match('/^[a-zA-Z0-9_. -]{1,23}$/', $elem)){
            die("One of element  name is not valid");
        }
    },
    $array
);
cweiske
  • 30,033
  • 14
  • 133
  • 194