Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6860025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:21:25+00:00 2026-05-27T02:21:25+00:00

I used an online regular expression (regex) tester to build a simple regex, however

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:21:26+00:00Added an answer on May 27, 2026 at 2:21 am

    You need to escape the / inside the regex, because / is also being used as delimiter:

    if (preg_match('/(^Keyword1\/$|^Keyword2\/$)/', $input, $matches))
                               ^            ^
    

    Alternatively, use a different delimiter:

    if (preg_match('~(^Keyword1/$|^Keyword2/$)~', $input, $matches))
                    ^                         ^
    

    Since both your sub-regexs have common anchors, you can simplify your regex as:

     if (preg_match('~^(Keyword1|Keyword2)/$~', $input, $matches))
    

    Why the warning in your regex?

    '/(^Keyword1/$|^Keyword2/$)/'
    

    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 like s, m, i after the closing delimiter. But in your case, PHP sees a $ after the closing delimiter. Since $ is not a valid modifier you get the warning:

    PHP Warning: preg_match(): Unknown modifier ‘$’ in …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used the following function I found online and it works perfectly. However, when
Up until now I've only used my online server of PHP and MYSQL. I'm
I'm following a php video tutorial (it's not online sorry) from Lynda.com and used
I want to create the membership tables online that are used for ASP.Net 3.5
I have a regular expression that looks through html content for some keywords that
I'm borrowing a rather complex regex from some PHP Textile implementations (open source, properly
I have wamp installed on my pc. and i have used the put online
I have been thinking about a regular expression that can transform a list like
I have 3 sliders that are used calculate a number of MB used online.
I used an online HTML maker to get this <p><span style=\font-family: arial, helvetica, sans-serif;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.