0

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?

0

1 Answer 1

0

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(...))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.