I need to check if a string is a valid regex.
Is there any way to do it without having performance issues?
N.B: I want to check it BEFORE using it, preg_last_error() is not what I’m looking for.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The suggested
preg_last_errorwon’t work for you because it only return PCRE runtime errors, not compilation faults – in the latter case all you get is a php warning (“Compilation failed”). A clean way to work around this is to install an error-to-exception error handler, and then compile the pattern within a try-catch block: