简体   繁体   中英

Yii url rule with regular expression

I have a rule:

'employee/renewOffer/<id_offer:\d+>;<title:.+>' => 'company/renewOffer',

http://www.example.com/123;title-of-post

which concerns to any SINGLE offer and this works great, but how this rule should be look if if select MULTIPLE offers. Link will look like this (comma separated ID's):

http://www.example.com/123,456,12,5;title-of-post

As recommended by ndn, you can allow for commas by replacing the \\d+ in your rule with [\\d,]+ . In other words, change the rule to this:

'employee/renewOffer/<id_offer:[\d,]+>;<title:.+>' => 'company/renewOffer',

The square brackets create a character class (any of the characters listed inside the brackets are allowed). So, by putting \\d and , in the character class, that allows for any digit or any comma to match. However, that solution only specifically allows for digits and commas. If you want it to be more flexible, you could change it to accept any character by using . . For instance:

'employee/renewOffer/<id_offer:.+?>;<title:.+>' => 'company/renewOffer',

The ? after the .+ just makes the + non-greedy. That means it will capture as little as it can rather than as much as it can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM