1

I am using stripos to modify an active navigation class,

<?php if (stripos($_SERVER['REQUEST_URI'],'/members/login') !== false) {echo 'class="active"';} ?>

It works like a charm. However I need to add another REQUEST_URI to check in the string and cannot figure out how to properly format the code.

I have tried:

, '/members/login | /members/members'

and others without success.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280

2 Answers2

3

You'll just have to do it twice:

if(
   stripos($_SERVER['REQUEST_URI'],'/members/login') === 0
   ||
   stripos($_SERVER['REQUEST_URI'],'/members/members') === 0){ ...

Note that I switched to ===0 as I presume you wouldn't want '/someotherpartofyoursite/members/members' to match presumably. If you want it in 1 call, you can use regular expressions (see preg_match()), but this is fast & clear enough in my opinion.

If the list becomes longer, it depends on whether these are the whole paths, and if they are, something like this could be more suitable:

$urls = array('/members/login','/members/members');
if(in_array(parse_url($_SERVER['HTTP_REQUEST_URI'], PHP_URL_PATH),$urls)){....

... but not knowing your url scheme that's a guess.

Wrikken
  • 69,272
  • 8
  • 97
  • 136
2

You can do that in single call to preg_match as well like this:

if (preg_match('#/members/(?:login|members)#i', $_SERVER['REQUEST_URI'])) {
    // matched
}
anubhava
  • 761,203
  • 64
  • 569
  • 643