URI Routing > Regex help. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: URI Routing > Regex help. (/showthread.php?tid=32030) |
URI Routing > Regex help. - El Forum - 07-10-2010 [eluser]Killswitch[/eluser] Hey guys, long time no talk, been busy working. My problem is I'm setting up my site uri routes to allow the following formats. mysite.com/category mysite.com/category/page mysite.com/catgory/item-slug mysite.com/7randchars So far I got the first 3 down, I can easily make my site go to the right controllers and methods doing that, but the last one, /7randchars is sorta like a key... What it does is allow my site to have it's own "tiny url" feature for Twitter and sharing, but for some reason I can't figure out how to restrict the uri section to only 7 chars before taking it to be a category or something. Any ideas? I want to restrict it to only 7 characters, and Alphanumerical characters that can be upper and lower case. Thanks in advance. Regular Expressions isn't my strong point URI Routing > Regex help. - El Forum - 07-11-2010 [eluser]treeface[/eluser] Code: $route['([a-zA-Z0-9]{7})'] = "mycontroller/get_something_by_id/$1"; That should do it. Get 7 alphanumeric characters, regardless of case, capture them (later referred to as '$1'), and redirect it to the uri of your choice. I only did a little bit of testing, but it seems to work. Something you may want to think about first: what happens when you put together a "product" controller and forget that "product" is 7 characters long? You might want to have your short URLs with something like "s/([a-zA-Z0-9]{7})" such that it looks like "s/fH4J80x". URI Routing > Regex help. - El Forum - 07-11-2010 [eluser]Killswitch[/eluser] [quote author="treeface" date="1278850680"] Code: $route['([a-zA-Z0-9]{7})'] = "mycontroller/get_something_by_id/$1"; That should do it. Get 7 alphanumeric characters, regardless of case, capture them (later referred to as '$1'), and redirect it to the uri of your choice. I only did a little bit of testing, but it seems to work. Something you may want to think about first: what happens when you put together a "product" controller and forget that "product" is 7 characters long? You might want to have your short URLs with something like "s/([a-zA-Z0-9]{7})" such that it looks like "s/fH4J80x".[/quote] Ah, I don't know what I did wrong but that works, and the whole products thing isn't a worry as the category stuff is restricted to only alpha characters and not numeric so only thing that has alphanumeric is the shorturl. |