Validate a pattern

You can check whether a pattern is valid with pattern()->valid() method.

pattern('I am a valid pattern')->valid();
true

Remember, pattern()->valid() doesn't throw any exception nor issues any warnings. It only returns true/false.

pattern('I am an (( invalid }} pattern')->valid();
false

Old-school patterns

Undelimited patterns can be validated with pattern()/Pattern::of(). To validate old-school patterns (e.g. /Hello/i), use Pattern::pcre():

Pattern::pcre('/I am an valid pattern/m')->valid();
true
Last updated on