CodeIgniter Forums
Need routing help, specifically with regex - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Need routing help, specifically with regex (/showthread.php?tid=7582)



Need routing help, specifically with regex - El Forum - 04-15-2008

[eluser]ehicks727[/eluser]
Hello! I'm building a directory with entries by state, county, and city. I've got the state and county working, but need some help with the city part of it. Here's my routing.

Code:
$route['insurance/([A-Z][A-Z])/(:any)'] = 'insurance/county/$1_$2';
$route['insurance/([A-Z][A-Z])/(:any)/(:any)'] = 'insurance/city/$1_$2_$3';

I'm guessing that the first (:any) is gobbling up the second. The ([A-Z][A-Z]) represents a state code.

It should go in this order:
insurance/statecode/county
or
insurance/statecode/county/city

Is there a regex that I can use instead of :any that excludes a forward slash? The only characters allowed should be A-Z, a-z, 0-9, and a dash (-) in the county or city.

Thanks for the help. If you need me to explain my question a little better, please just ask and I'll try to clarify.


Need routing help, specifically with regex - El Forum - 04-15-2008

[eluser]xwero[/eluser]
Switch the routes so that the most specific route becomes the first and it will be fine

[A-Z][A-Z] can be written as [A-Z]{2}

The other regex you are looking for is [A-Za-z0-9\-]+


Need routing help, specifically with regex - El Forum - 04-15-2008

[eluser]ehicks727[/eluser]
That did the trick! Thanks much.


Need routing help, specifically with regex - El Forum - 04-15-2008

[eluser]Leggy[/eluser]
Well if you really want any character except /n (newline) you could use (.+)

Code:
$route['insurance/([A-Z]{2})/(.+)'] = 'insurance/county/$1_$2';
$route['insurance/([A-Z]{2})/(.+)/(.+)'] = 'insurance/city/$1_$2_$3';