preg_last_error() not working correctly
Closed this issue · 1 comments
php
<?php
preg_match('/(\d+/', '');
if (preg_last_error() === 1) printf("Error detected\n");
php output
» php regex.php
PHP Warning: preg_match(): Compilation failed: missing closing parenthesis at offset 4 in /home/flip111/regex.php on line 3
Error detected
psysh
>>> preg_match('/(\d+/', '');
PHP Warning: preg_match(): Compilation failed: missing closing parenthesis at offset 4 in /home/flip111/.configeval()'d code on line 1
>>> if (preg_last_error() === 1) printf("Error detected\n");
>>>
There are more functions in PHP that retreive the last error not just about PCRE. I only tested about the PCRE one but i'm worried about the other error functions.
PsySH is written in userland code, which means things like preg_last_error
and json_last_error
can be polluted by library code and not return the value you expect 🙁
Unfortunately I don't think there's a great answer here. We use json_encode
to print return values, among other things, and while it would be a bit annoying, we could replace it with our own escaper. But we use regular expressions in a ton of places, and they're not as straightforward to replace, so that one's a bit more problematic.
The one workaround would be that within a single input, the result will always be correct: