1

I'm pretty close from what I want to achieve.

I have this :

'/create-group/([A-Za-z0-9\-\_]+)(?:/([A-Za-z0-9\-\_]+))?(?:(\?|&)(groupid|copyofgroup|ref)=([A-Za-z0-9\-\_]+))?'

It allows me to restrain what URIs can be typed in and accessed, such as:

/create-group/123/456?groupid=789

In the last bit of the regex, I allow 3 parameters (groupid, copyofgroup and ref).

This is all good, but I can't figure out a way to have my preg_match validate an infinite number of these 3 parameters. Right now, I'm only limited to 1 instance of a parameter.

I'd like my preg_match() to accept :

/create-group/123/456?groupid=789&ref=101112&copyofgroup=789

I've tried multipliers such as * and +, but to no avail.

Thank you!

4
  • 1
    Why are you using regex and not $_GET, $_POST or $_REQUEST superglobals? Commented Jan 20, 2016 at 19:48
  • @Nordenheim I'm not sure what you mean? This code is place at the top-level before fetching the relevant files. I just want to restrain what parameters can be inserted in the URI. How would $_GET superglobals help me? Commented Jan 20, 2016 at 20:11
  • Replace [A-Za-z0-9\-\_] with [[:alnum:]_-]. Characters lose their specialness inside square braces, so you don't need to escape things with backslashes, and a dash at the either end means a dash rather than a range delimiter. And character classes just make sense. Commented Jan 20, 2016 at 20:25
  • @Justin01 the tags suggest you use PHP, meaning you can $_GET the needed parameters from the URI string and ignore the rest instead of crutching your way through the code. Commented Jan 20, 2016 at 20:28

2 Answers 2

1

You can add {0,3} instead of "?", at the end:

/create-group/([A-Za-z0-9-_]+)(?:/([A-Za-z0-9-_]+))?(?:(\?|&)(groupid|copyofgroup|ref)=([A-Za-z0-9-_]+)){0,3}

I tried here: https://regex101.com/

Sign up to request clarification or add additional context in comments.

1 Comment

Very interesting, don't know why I haven't thought of that. Thank you very much!
0

Found the solution :

'/create-group/([A-Za-z0-9\-\_]+)(?:/([A-Za-z0-9\-\_]+))?(?:((\?|&)(groupid|copyofgroup|ref)=([A-Za-z0-9\-\_]+))+)?'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.