phpregexphalconphalcon-routing

Pass regex flags to route in Phalcon micro app


I'm trying to define a route in a Phalcon Micro App, but can't figure out how to pass the global flag to the regular expression. Here is my route

/api/v1/product/detail/{sku:([\w\d]+-?)*[\w\d]*}

Expecting both of these to match, however the later requires the global modifier to match.

How can I specify a flag in the regular expression? I did not see this specified in the documentation.


Solution

  • Global modifier is set whenever you don't want to stop at first match and since you have one matching value at a time you don't need g modifier either.

    Let's go with an example. Suppose your current input string is this:

    /api/v1/product/detail/8Z-WEXN-CG0H
    /api/v1/product/detail/025-3bags
    

    Then your regex stops at first match since no g's applied but if your input string is one of these at a time:

    /api/v1/product/detail/8Z-WEXN-CG0H
    
    /api/v1/product/detail/025-3bags
    

    It works so you don't need g at all. And I'd suggest you to modify your regex into this simplified:

    \w+(?:-\w+)*
    

    As \w includes \d as part of a match.