Welcome Guest, Not a member yet? Register   Sign In
URI Routing > Regex help.
#1

[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 Sad
#2

[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".
#3

[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.




Theme © iAndrew 2016 - Forum software by © MyBB