problem with if (str_contains... operator

Discussion in 'PHP' started by oakfield, Dec 31, 2008.

  1. #1
    Hi,

    I've been having a nightmare with the if str_contains operator. I've had a look throughthe forums and the closest I've come to finding anything that will help out is: http://forums.digitalpoint.com/showthread.php?t=122301 which nearly does it, but unfortunately not quite.

    My code at the moment is:

     foreach ((get_the_category()) as $cat)
    		 if (str_contains($_SERVER['REQUEST_URI'],
    $cat->$category_nicename){ echo ('hello there');} else {echo ('booo');}
    
    PHP:
    In essence what I'm looking for is if the URI contains the category_nicename display one set of info and if it doesn't then another set, but I'm really struggling with it.

    Ultimately what I'm wanting to do with this is if the URI does contain the category_nicename display links in the header of the page to the RSS and Atom feeds for that category, and if not, display the site's default RSS feeds, but the rest of it is a question for another day...

    Any help is very welcome!
     
    oakfield, Dec 31, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
  3. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I took a look at the thread you linked to. str_contains is not a built-in function, which I of course knew already, but I found on that page what made you think there was. That person made a custom wrapper function (by the way, something like str_contains() would be called a function, not an operator; an operator would be like <, >, <=, >=, ==, !=, ===, !==, &&, ||, ++, --, etc).

    <?php
    function str_contains($haystack, $needle, $ignoreCase = false) {
       if ($ignoreCase) {
           $haystack = strtolower($haystack);
           $needle  = strtolower($needle);
       }
       $needlePos = strpos($haystack, $needle);
       return ($needlePos === false ? false : ($needlePos+1));
    }
    ?>
    PHP:
    You'd have to have that code somewhere in your file so that it defines what the str_contains function is. Really do not need that wrapper function though.. just use the built-in PHP strpos() function, which Danltn kindly linked to, which is what that str_contains() wrapper uses anyways.
     
    zerxer, Jan 1, 2009 IP