I used an online regular expression (regex) tester to build a simple regex, however using PHP’s preg_match it’s giving an unknown modifier for $.
Here is the regex:
if (preg_match('/(^Keyword1/$|^Keyword2/$)/', $input, $matches))
I’m trying to check if $input equals either Keyword1/ or Keyword2/ (exact match). I know I can easily do this with "if ($input == ‘Keyword1/’)", however I’d rather have a few lines of regex vs. a dozen if statements in the code.
How can I fix it?
You need to escape the
/inside the regex, because/is also being used as delimiter:Alternatively, use a different delimiter:
Since both your sub-regexs have common anchors, you can simplify your regex as:
Why the warning in your regex?
Since you are using
/as delimiter, the second/in your regex makes PHP think it is the end of your regex. Now PHP accepts regex modifiers likes,m,iafter the closing delimiter. But in your case, PHP sees a$after the closing delimiter. Since$is not a valid modifier you get the warning: