0

I need some help with a PHP based path parser, I am trying to create a path router that grabs arguments form the given string and returns an keyed array containing the results. I can do it inside of a whole loop where each area is broken down and tested seperatly but I feel that there should be a way to do it as a signaler regex that then loops though the results

users/<id:(\d+)>/ = users/1 || users/42 != users/bob
    returns args['id'] = 1 || args['id'] = 42

user/register
    args = []

name/<fname:([a-zA-Z]{1,10})>/<lname:([a-zA-Z]{1,10})> = name/bob/smith || name/jordan/freeman
    args['fname'] = bob     || args['fname'] = jordan
    args['lname'] = smith   || args['lname'] = freeman
5
  • 1
    If you have it working with a while-loop, don't eff it up with regex. Commented Dec 4, 2014 at 5:24
  • 1
    There's nothing at all wrong with pursuing a regex solution if OP believes it will be faster and more effective than a while loop. As long as the pattern is clear, regex is probaly ideal for this and will not "eff" anything up. (Wish I could help you OP. PHP ain't my game. Commented Dec 4, 2014 at 5:50
  • 1
    Thanks, the reason behind not wanting use a loop is that this function will be called in a already nested loop, and will be parsing several hundred paths. not an ideal candidate for a loop but named captures php 5.2 addition was exactly what I was looking for. I found it from a related post stackoverflow.com/questions/4777635/… Commented Dec 4, 2014 at 5:55
  • Sounds like a bad approach to me. If you have a high number of routes you are checking against, then you will be comparing each route against a number of regular expressions. This will likely be relatively slow as compared to say, parsing out the first segment of the route, figuring out what controller to hand it to, and then letting the controller tease out the rest of the actions/parameters in a way that it finds appropriate. Doing this by simple explode() would likely be much faster. Commented Dec 4, 2014 at 18:43
  • 1
    I thought about doing that, but in the end I settled on doing it this way because of additional needs. I am compiling the regexes and storing them in cache so I don't need them to be built on every execution which should reduce the performance loss and it adds a extra layer of security. as I can obscure the controller until after the path has already been validated it also allows 2 controller to use the same paths or one to have 2 different entry points. I admit that it fills a very specific need and you will likely not use this as part of a standard framework Commented Dec 4, 2014 at 20:12

1 Answer 1

1

Named captures is what I was looking for, just need to transform my markup or redo the patterning. http://www.regular-expressions.info/named.html

Here is what I can up with, IDK if it will help anyone else?

$str = 'user/1/';
$reg = "user\/(?P<ID>\d+)\/";

var_dump(testPath($str, $reg));

$str = 'user/bob/smith/';
$reg = "user\/(?P<fname>[a-zA-Z]+)\/(?P<lname>[a-zA-Z]+)\/";

var_dump(testPath($str, $reg));

function testURL($url, $reg){
    if(preg_match("/^$reg$/", $url, $matches)){
        return $matches;

    }else{
        return null;
    }
}
Sign up to request clarification or add additional context in comments.

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.