I want to validation multiple regex pattern. my patterns is
pattern1: /^4[0-9]{12}(?:[0-9]{3})?$/
pattern2: /^5[1-5][0-9]{14}$/
now how can i check both this pattern in single use in php?
If you need both regex validated, you can do :
function preg_match_all($value, ...$patterns){
$valid = true;
foreach($patterns as $pattern){
$valid = $valid && preg_match($pattern, $value)
}
return $valid;
}
preg_match_all('', '/^4[0-9]{12}(?:[0-9]{3})?$/', '/^5[1-5][0-9]{14}$/'); // false
But this is not really clean, don't forget you can just do something that dont hurt readability of your code (logicals operators)
if(preg_match(...) && preg_match(...))
if(preg_match(...) || preg_match(...))
if(preg_match(...) xor preg_match(...))