开发者

Regex to detect a proper permalink?

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜