tags:

views:

34

answers:

1

These permalinks above are rerouted to my page:

page.php?permalink=events/foo
page.php?permalink=events/foo/
page.php?permalink=ru/events/foo
page.php?permalink=ru/events/foo/

The events is dynamic, it could be specials or packages.

My dilemma is basically; I need to detect an empty link in order so I can feed a robots no index meta tag in the case of:

page.php?permalink=events
page.php?permalink=events/
page.php?permalink=ru/events/
page.php?permalink=ru/events

I can't use a simple pattern such as [a-zA-Z]+\/?(.+)/ since it won't work on the i18n permalinks.

What regex could I use which would detect this, using $_GET['permalink'] as the reference to the permalinks? And avoid false positives?

Update:

Empty link means there's no fragment after the "events/" part. These are empty:

page.php?permalink=events
page.php?permalink=events/
page.php?permalink=ru/events/
page.php?permalink=ru/events
+1  A: 

I think you are close:

$pattern = '#^(?:[a-z]{2}/)?[a-z]+/(.+)/$#i';

Explanation:

#               - regex start
  ^             - start-of-string anchor
  (?:           - non-capturing group (I18N)
    [a-z]{2}    - 2 letter language code
    /           - a slash
  )?            - end non-capturing group, make optional
  [a-z]+        - any letter a-z, multiple times (event)
  /             - a slash
  (.+)          - group 1: any character, multiple times
  /             - a slash
  $             - end-of-string anchor
#i              - regex end, make case-insensitive
Tomalak
Thanks for the contribution. `events/permalink` would fail unless you added a `?` after the last `/` and I think `ru/events/` would still pass. If it's not possible doing it like this, perhaps I could just make a group of words in the place of where `events` would be captured? eg (?:events|packages|specials) so it's a bit easier?
meder
What about something as simple as `#[^/]{3,}/[^/]+/?$#`
meder
@Fedor: You did *still* not exactly define your requirements. Please add to your question a definitive list of examples that show which strings should match and which should not.
Tomalak
The stuff I had in the code blocks defines which should match and which shouldn't.
meder