2

I am trying to hide a link on my navigation bar on certain pages of my website. I have a system which works for one page, but when I try to add more than one string to the if statement, it does not work. Can anyone offer advice on how to fix this?

Working statement which hides the link on one page:

<?php
    $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

    if (strpos($url, 'terms-of-use') !== false) {

    }

    else {
        echo '<li class="nav-item"><a class="nav-link" href="'. basename($_SERVER['PHP_SELF']) .'" title="Contact Us">Contact Us</a></li>';
    }
?>

I have tried adding the following:

if (strpos($url, 'terms-of-use', 'case-studies', 'cookie-policy', 'privacy-policy') !== false) {
2
  • BTW… instead of "if … do nothing else do something", simply invert the condition: if (strpos(…) === false) { echo … }! Commented Dec 15, 2016 at 10:14
  • @deceze I tried this and it removed the link on every page, not just the pages in the list. Commented Dec 15, 2016 at 10:21

2 Answers 2

1

For checking for multiple values that one string contains, you can use Regex.

Like this:

if(preg_match('(foo|bar)', $baz) === 1) { } 

This will check if $baz contains "foo" and "bar".

Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly. Thank you so much. I will mark this as the Answer in 7 minutes. (I have to wait)
0

This should work too:

$needles = array('terms-of-use', 'case-studies', 'cookie-policy', 'privacy-policy');
foreach($needles as $what) {
    if(($pos = strpos($url, $what)) !==false ) {
        return true;
}
return false;

}

1 Comment

This doesn't work but the concept of what you are trying is exactly what I am trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.