Liam Liam - 1 year ago 115
PHP Question

How do I catch an invalid preg_match pattern?

I am writing a PHP script that accepts a regular expression pattern from the user which is used by preg_match(). How can I check that the pattern is valid?

Answer Source

Just test it. preg_match() will return FALSE if the pattern is not valid.

Return values: preg_match() returns the number of times pattern matches. That will be either 0 times (no match) or 1 time because preg_match() will stop searching after the first match. preg_match_all() on the contrary will continue until it reaches the end of subject. preg_match() returns FALSE if an error occurred.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download