oliverbj
oliverbj

Reputation: 6052

PHP - Preg match to match URL

I have the following URL format:

http://www.domain.com/admin/?i=page

And on sub-pages, this is the format:

http://www.domain.com/admin/?i=page&n=subpage

When echoing $_SERVER["REQUEST_URI]; I get the following:

/admin/?i=page

I am trying to show an .active class whenever the page is active like this:

if(preg_match ('#^/admin/?i=page', $_SERVER['REQUEST_URI'])){echo "active";}

However, the active class isn't triggered. What am I doing wrong?

Upvotes: 0

Views: 145

Answers (2)

Itamar
Itamar

Reputation: 59

You are missing a delimiter in the end of the regular expression. Moreover , '?' should be escaped :

#^/admin/\?i=page#

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

  1. You forget the ending php delimiter.
  2. You forget to escape ?

    if(preg_match ('#^/admin/\?i=page#', $_SERVER['REQUEST_URI'])){echo "active";}
    

Upvotes: 2

Related Questions