-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,655
  • 10
  • 37
  • 44
beginner
  • 1,770
  • 2
  • 24
  • 46

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
  • 28,704
  • 13
  • 124
  • 186