I am trying to set up a service similar to rubular, but with PHP as the language using the preg family of functions. It will take an input regex, a test string, and run preg_match()
.
How can I find out if a compilation error has occurred (eg: invalid regex), and if that is the case, what was the error? Normally it will throw warnings like:
Warning: preg_match() [function.preg-match]: Compilation failed: missing ) at offset x in ****** on line y
pcre_last_error()
is totally useless here, since it will return 0 (PREG_NO_ERROR
) if the regex fails to compile.
One option I am considering is to use output buffering to capture the warning, but there's got to be a better way.