1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

If url contains X then ..

Discussion in 'PHP' started by Dmak02, Sep 28, 2008.

  1. #1
    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:

    SEMrush
     
    Dmak02, Sep 28, 2008 IP
    SEMrush
  2. NetworkTown.Net

    NetworkTown.Net Well-Known Member

    Messages:
    2,022
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    165
    #2
    post the part of the code so i can suggest what method to use.
     
    NetworkTown.Net, Sep 28, 2008 IP
  3. AnonymousUser

    AnonymousUser Peon

    Messages:
    593
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    AnonymousUser, Sep 28, 2008 IP
  4. Dmak02

    Dmak02 Peon

    Messages:
    494
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not sure if this would work that well, but here is my current code.

    <?php if ($_SERVER['REQUEST_URI']=='/pokemon/' ) {echo 'class="selected"';} ?>
    PHP:
     
    Dmak02, Sep 28, 2008 IP
  5. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    <?php if (stripos($_SERVER['REQUEST_URI'],'/pokemon/') !== false) {echo 'class="selected"';} ?>
    
    PHP:
     
    Shoro, Sep 28, 2008 IP
  6. EI-SD

    EI-SD Guest

    Best Answers:
    0
    #6
    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+.
     
    EI-SD, Sep 28, 2008 IP
  7. Dmak02

    Dmak02 Peon

    Messages:
    494
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks! that worked flawlessly!
     
    Dmak02, Sep 28, 2008 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    if(preg_match('/\/pokemon\//',$_SERVER['REQUEST_URI'])) echo 'class="selected"';
    PHP:
    Could use this too
     
    JAY6390, Sep 28, 2008 IP
  9. AnonymousUser

    AnonymousUser Peon

    Messages:
    593
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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
     
    AnonymousUser, Sep 28, 2008 IP
  10. jdryden

    jdryden Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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
     
    jdryden, Jun 24, 2010 IP
  11. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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
     
    JAY6390, Jun 24, 2010 IP
  12. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #12
    In other way it can also be said as if the URL is equal to $_SERVER['REQUEST_URI'],'/pokemon/ then it prints class='selected' :)
     
    roopajyothi, Jun 24, 2010 IP
  13. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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
     
    JAY6390, Jun 24, 2010 IP