-3
<?php
if (!preg_match("{(?:login|logout|register)\.php$}", $_SERVER["PHP_SELF"]))
{    
    if (empty($_SESSION["id"]))
    {
        redirect("login.php");
    }

 }
?>

Couldn't understand how my 1st if conditions is checking pages other than login.php, logout.php, register.php .. What ?: is doing?

ASEN
  • 167
  • 10
  • 2
    See http://www.regular-expressions.info/refcapture.html – deceze Aug 22 '14 at 13:14
  • This kind of duplicate is admittedly almost impossible to find, but please do try to look through some sort of documentation first. – deceze Aug 22 '14 at 13:20
  • @deceze very easy if you check this [reference](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) :) – HamZa Aug 22 '14 at 13:23
  • 2
    @HamZa Try finding *that* one when searching for "?:". :) – deceze Aug 22 '14 at 13:25
  • 3
    @deceze Try this [query](http://stackoverflow.com/search?tab=votes&q=[regex]%20%22%3f%3a%22) :D (but I get your point) – HamZa Aug 22 '14 at 13:28

1 Answers1

0

?: is a non capturing group ie, it matches the expression but do not capture it.

Non-capturing parentheses group the regex so you can apply regex operators, but do not capture anything.

For example:

If I dont want to capture the protocol like http then see i will use the below regex

(?:http|ftp)://([^/\r\n]+)(/[^\r\n]*)?

Now, my result looks like this:

Match "http://google.com/"
     Group 1: "google.com"
     Group 2: "/"

Match "http://google.com/ABCD/XYZ/PQR"
     Group 1: "google.com"
     Group 2: "/ABCD/XYZ/PQR"
Rahul Tripathi
  • 152,732
  • 28
  • 233
  • 299