Welcome Guest, Not a member yet? Register   Sign In
Regex/Routing Question
#1

[eluser]fesweb[/eluser]
I'm trying to keep old URL's alive on a rebuilt site, and I want to treat them differently based on one segment.

Routes:
Code:
// the second segment will always contain [i]some[/i] text
// but if it contains numbers also, then I want to send it to a detail
$route['people/( numbers and text )/(.*)'] = "detail/$1/$2";

// otherwise, just send them to the list
$route['people/(.*)'] = "list/$1";
Vice-versa would be fine with me too; as in test for letters-only.

Anyway, regex and I have never -- and I mean never, ever -- gotten along, and I've spent some time trying a bunch of things, but now I need some help so that I can move on.

Any takers?

Thanks,
Matt
#2

[eluser]TheFuzzy0ne[/eluser]
Code:
// the second segment will always contain [i]some[/i] text
// but if it contains numbers also, then I want to send it to a detail
$route['people/([a-zA-Z0-9]+)/(.*)'] = "detail/$1/$2";

// otherwise, just send them to the list
$route['people/(.*)'] = "list/$1";

Please let me know how you get on.
#3

[eluser]fesweb[/eluser]
Thanks, Fuzzy - I had already tried that, and it doesn't filter on the right info (it can't tell if something has numbers, or is letters only).

So, I finally figured out what I was doing wrong, and here is the solution.

Very easy, once I ignored the bad info I got on some random site...
Code:
// this bit finds only letters and we treat those one way
$route['people/([a-zA-Z]+)'] = "list/$1";
$route['people/([a-zA-Z]+)/(.*)'] = "list/$1/$2";

// now we take care of all the other ones...
$route['people/(.*)'] = "detail/$1";
Thanks again.
#4

[eluser]TheFuzzy0ne[/eluser]
I'm confused. Please could you give me a few examples of each type of URL, and where you want it to be rerouted to? If I can see that, I might be able to help a bit more.

Thanks.
#5

[eluser]fesweb[/eluser]
No worries - it's already solved.

The patterns were...

for directory listings:
/people/textonly/
/people/textonly/more-text-or-whatever

for individual details:
/people/1234-some-text
/people/1234-some-text/more-text-or-whatever

So, I had to separate the two types based on whether there were numbers in segment(2), or just text.

It was easiest to check that only letters were in the second segment. So, once I got the basic [a-zA-Z]+ bit working. It was fine.

Code:
// this finds the listings
$route['people/([a-zA-Z]+)'] = "profiles/$1";
$route['people/([a-zA-Z]+)/(.*)'] = "profiles/$1/$2";

// this takes care of the details
$route['people/(.*)'] = "profile/$1";

Anyway, it works perfectly.
#6

[eluser]TheFuzzy0ne[/eluser]
Excellent. Well done!




Theme © iAndrew 2016 - Forum software by © MyBB