Matthew
Matthew

Reputation: 15642

Simple regex question (php)

I've been using this line in a routes file:

$route['^(?!home|members).*'] = "pages/view/$0";

The string in the array on the left of the expression ( ^(?!home|members).* ) is what I'm trying to figure out.

Basically any url that is not:

/home or /home/ or /members or /members/ should be true. The problem I have is if the url is something like /home-asdf. This counts as being in my list of excluded urls (which in my example only has 'home' and 'members'.

Ideas on how to fix this?

Upvotes: 0

Views: 64

Answers (3)

chrisgooley
chrisgooley

Reputation: 774

As an aside, I use this all the time and it works wonders: http://www.rubular.com/ Its predominantly for ruby but works well when working out general regex for php etc too.

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361605

Try this modification:

^(?!(home|members)([/?]|$)).*

This filters out URLs beginning with home or members only if those names are immediately followed by a slash or question mark ([/?]), or the end of the string ($).

Upvotes: 1

Dan Grossman
Dan Grossman

Reputation: 52372

http://www.regular-expressions.info/

The dot . operator matches all characters. The * operator means the previous pattern will be repeated 0 or more times. That means the end of your route matches any character any number of times after the word home or members. If you only want to match one or zero slashes, then change .* to /?.

Upvotes: 0

Related Questions