Right now, my code checks the url for x, so if url == to x then... So the condition is true if the url is Digitalpoint.com/x/ but it breaks if the url is digitalpoint.com/x/y So how do I check if url contains x? Update: Here's the code <?php if ($_SERVER['REQUEST_URI']=='/pokemon/' ) {echo 'class="selected"';} ?> PHP:
Simple, run this function on your variable that checks the first character as "x" $X = substr($YourXVariable, 0, 1); if($X == "x") { echo "X is there baby s*@t yeah!; } else { echo "Nope"; } PHP:
Not sure if this would work that well, but here is my current code. <?php if ($_SERVER['REQUEST_URI']=='/pokemon/' ) {echo 'class="selected"';} ?> PHP:
AnonymousUser's code is wrong. It should be programmed like this: (Assuming your URL is $url) $url = "Your URL"; $search = "What you're searching for"; $count = 1; //Number of forward slashes before the code "breaks" if (strstr($url, $search)) { $split = split($url, "//"); if (count($split) < $count) { echo 'Success'; } } PHP: The above code is compatible with PHP 4+.
if(preg_match('/\/pokemon\//',$_SERVER['REQUEST_URI'])) echo 'class="selected"'; PHP: Could use this too
My code isnt "wrong", I posted it before he posted his code, He re-edited his main post, So I had nothing to "work" on, which was why I told him to apply is to his $XVariable, which he doesnt have, anyway some1s posted the right code so its all good
Nice work! This does work flawlessly! But I have a question about it - what's the "!== false" do? Does that mean that the function "stripos" does the "echo" if it CANNOT find the URL specified, in this case "/pokemon", so the !false part reverses that? I'm learning PHP so I probably have got it wrong - but I can't understand what it's for! Thanks! James
Because of PHP's loose variable interchangeability when stripos returns that the position of the string is found at character 0 (ie it returns 0) then this can be interpreted as false with just == or !=. What === or !== does is check the type of the value as well as the actual value, therefore eliminating 0 being equivalent to 0, and you being able to determine if a string is at position 0 or not in the string at all
In other way it can also be said as if the URL is equal to $_SERVER['REQUEST_URI'],'/pokemon/ then it prints class='selected'
Not quite, it just sees if it can match a portion of the first string with the second one (without case sensitivity). If you were just testing if one is equivalent to the other you would just use == in an if test