Test if a regular expression is a valid one in PHP

前端 未结 6 1922
太阳男子
太阳男子 2020-12-16 14:24

I am writing a form validation class and wish to include regular expressions in the validation. Therefore, the regex provided isn\'t guaranteed to be valid.

How can

6条回答
  •  轮回少年
    2020-12-16 15:15

    Anyone still looking at this question anno 2018, and is using php 7, should be using try/catch.

    try { 
        preg_match($mypattern, '');
    } catch (\Throwable $exception) {
        // regex was invalid and more info is in $exception->getMessage()
    }
    

提交回复
热议问题