phpstripos

Using stripos($_SERVER['REQUEST_URI'] for an active state on a list of URL


I have this to add an active state on a navigation

<li <?php if (stripos($_SERVER['REQUEST_URI'],'/page') !== false) {echo 'class="active"';} ?>><a href="/page">Page</a></li>

But I need to add a few pages for the active state to trigger. Something like:

<li <?php if (stripos($_SERVER['REQUEST_URI'],'/page', '/another', '/somethingelse') !== false) {echo 'class="active"';} ?>><a href="/page">Page</a></li>

Solution

  • You might use pattern matching with preg_match:

    if ( preg_match(
            '/\/(page|another|somethingelse)/', // any regexp here
            $_SERVER['REQUEST_URI']
         )
    ) { 
      echo ...
    }