D
D
Dmitry2018-06-18 17:11:55
PHP
Dmitry, 2018-06-18 17:11:55

How to check the validity of a regexp?

Is there a way to check the validity of a regexp expression in php?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Pushkarev, 2018-06-18
@By_Engine

function invalidRegex($regex)
{
    if(preg_match($regex, null) !== false)
    {
        return '';
    }

    $errors = array(
        PREG_NO_ERROR               => 'Code 0 : No errors',
        PREG_INTERNAL_ERROR         => 'Code 1 : There was an internal PCRE error',
        PREG_BACKTRACK_LIMIT_ERROR  => 'Code 2 : Backtrack limit was exhausted',
        PREG_RECURSION_LIMIT_ERROR  => 'Code 3 : Recursion limit was exhausted',
        PREG_BAD_UTF8_ERROR         => 'Code 4 : The offset didn\'t correspond to the begin of a valid UTF-8 code point',
        PREG_BAD_UTF8_OFFSET_ERROR  => 'Code 5 : Malformed UTF-8 data',
    );

    return $errors[preg_last_error()];
}

E
Eugene Chefranov, 2018-06-18
@Chefranov

https://regex101.com

N
Nikita Linberg, 2018-06-18
@zaisabaev

regex101.com
can be checked on this site

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question