This question already has an answer here:
I keep getting an error when using this and I am not sure why. Any help would be amazing. I have Googled it and found examples but I get the error even with other peoples examples.
$statement = $list[$i];
echo $statement;
preg_match("/$statement/i", $q)
I also tried this and neither work:
$statement = '/' . $list[$i] . '/i';
echo $statement;
preg_match($statement, $q)
The error I get is:
Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 0
When I echo out the $statement
I get: "/Who/i"
(without the quotes)
</div>
Make sure that whatever's in $statement
will actually produce a VALID regex, e.g.
$statement = '(a|'; // note lack of closing )
preg_match("/$statement/", $text);
will actually produce the regex
/(a|/
which is invalid, because there's no closing )
to finish off the capture group. You can get around this with:
$statement = preg_quote('(a|');
^^^^^^^^^^
which will escape any regex metacharacters so you produce a valid regex in the end.
Essentially, you're probably suffering from the regex equivalent of an SQL injection attack.