Welcome Guest, Not a member yet? Register   Sign In
Anyone good with Regex?
#1

[eluser]mast3rpyr0[/eluser]
Hey guys, I'm trying to create a $route that will take a string of any 5 character, letters or numbers (IE. n3AdB, case-sensitive), string and route it to a controller's function. These codes will then be looked up in a database. I have probably the most minute understanding of how a regex works so this is a bit difficult for me, but so far I've come up with:

Code:
$route['[A-Za-z0-9]\{5\}'] = "codes/getCode/$1";

This doesnt seem to work at all and just sends me to a 404.

Is there anyone here thats good at these, or maybe have another alternative? It also needs to ignore strings that are actually the names of controllers, however if needed i can just not make any controllers have 5 character names.

Thanks in advance
#2

[eluser]InsiteFX[/eluser]
RegExr - Desktop see bottom right side to download desktop version
#3

[eluser]mast3rpyr0[/eluser]
Thanks, i'll play with that, it looks fun haha.

Still excepting final answers though just incase I fail hard at this xD
#4

[eluser]Aken[/eluser]
You're pretty close.

1) The backslashes are escaping the {} characters, meaning the URL is now looking for those literal characters. To match the total of 5 characters, just write it as {5}.
2) In order to use $1 in the second part of the route, you need to tell the regular expression to save something as a back reference using parentheses.

Code:
$route['([a-zA-Z0-9]{5})'] = 'codes/getCode/$1';

Note: this will route any five character request, even if it is a normal controller somewhere else in your application. For example, your controller "codes" would match that route. CI does not check for an existing controller before routing the request, so I'd suggest adding some sort of unique prefix or something to the URL you want to route.
#5

[eluser]mast3rpyr0[/eluser]
I just came to that exact thing! xD and it works perfectly. Il just not use 5 character names for controllers, and i'm still able to create a route for 'login' if i want to as well before the regex one and it will still work fine.

Thanks guys! Bookmarking that tool.
#6

[eluser]InsiteFX[/eluser]
You can also download the tool if you look at the bottom right you will see desktop click on that and you can download it to system and run it anytime! Requires Adobe air to run...




Theme © iAndrew 2016 - Forum software by © MyBB