Is there a good way of test if a string is a regex or normal string in PHP?
Ideally I want to write a function to run a string through, that returns true or false.
I had a look at preg_last_error()
:
<?php
preg_match('/[a-z]/', 'test');
var_dump(preg_last_error());
preg_match('invalid regex', 'test');
var_dump(preg_last_error());
?>
Where obviously first one is not an error, and second one is. But preg_last_error()
returns int 0
both times.
Any ideas?
'invalid regex'
is a valid regex - in fact, any string that doesn't involve the regex special characters will be a valid regex. Try'['
instead.'['
is not a valid regex, therefore not all strings are valid regexs.'invalid regex'
is actually not a valid regex, PHP returnsWarning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in D:\xampp\htdocs\overheard\test.php on line 5
.