WEB Im stressing out trying to figure out this PHP conditional statement

Discussion in 'OT Technology' started by Ricky, May 3, 2009.

  1. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    So basically this is a rundown of what i want it to do:

    It gets a link and removes all the letters after a certain character. This link may alternate bewteen:

    www.google.com/?search=MYSEARCH

    and

    www.google.com/?search=MYSEARCH&BLAHBLAH

    I just want to be able to get "MYSEARCH"

    So i wrote this up

    Code:
    $link1 = "http://www.google.com/?search=MYSEARCH&BLAHBLAH";
    $link2 = "http://www.google.com?search=MYSEARCH";
    if (($pos2 = strpos($link1, '=')) !== false)
    {
    $link3 = substr($link1, $pos2);
    $link4 = substr($link3,1);
    }
    
    
    if (($pos3 = strpos($link4, '&')) !== false)
    {
    $link5 = substr($link4, 0, $pos3);
    }
    
    echo $link5
    
    So basically it works if i use the link1 variable because its running both my statements. But if its using link2, it wont be able to echo my results because the link5 variable will only run if it contains the "&".

    So i figure i have to use some sort of other conditional statement, such as

    Code:
    if ($link5==""){
    $finished = $link4;};
    
    if ($link4==""){
    $finished = $link;}
    but thats not even working.

    any ideas?
     
  2. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    and that php is probablly extremely messy but im pretty new :o

    and i wanted to add that i also tried using the elseif:

    Code:
    if ($link5==""){
    $finished = $link4;
    }
    
    elseif ($link4==""){
    $finished = $link;}
    
     
  3. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    ive been playing around and think maybe this is a better approach but for some reason i cant get it to work.

    Code:
    
    if (($pos2 = strpos($link1, '=') !== false)) && (($pos3 = strpos($link4, '&') !== false)
    {$finished = script to remove everything before = and everything after &}
    
    elseif (($pos2 = strpos($link1, '=') !== false))
    {$finished = script to just remove and }
    
    echo $finished;
    
     
  4. intrktevo

    intrktevo New Member

    Joined:
    Oct 18, 2004
    Messages:
    5,781
    Likes Received:
    0
    Location:
    UCF
    Last edited: May 3, 2009
  5. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    isnt that just to get the variable they enter in?

    I already have my stuff being posted to this. Im just having trouble with the conditional statement.

    unless you mean something else. If so please shed a little bit more details :o
     
  6. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    yes, but you can check it against an array that you set.
     
  7. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,767
    Likes Received:
    6
    just for the record incase anyone is intereted later on how i fixed it.

    i was too confused with what biowokauns and intrevero was saying so i just took my first variable and forced the & at the end.

    So even if the user enters in something that doesnt have the &, it'll add it thus going through both parts.

    If it already has an & it doesnt matter cause it'll still add one. I have my shit setup to count the first position of the &
     
  8. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,650
    Likes Received:
    15
    Location:
    Atlanta
    From what you wrote in post 1, sounds like you were looking for the value of SEARCH... which can be received by doing what intrktevo said:

    For example, if the URL was:

    OR

    You could find out what search is by doing the following:

    or is that not what you were looking for?
     
  9. noon

    noon get high and teach me how to listen

    Joined:
    May 4, 2002
    Messages:
    3,384
    Likes Received:
    0
    Location:
    Lawrence, KS
    or,

    Code:
    preg_match('#(\?|\&)?search\=([^&]+)#i', $url, $matches);
    $search = (count($matches) > 0) ? $matches[2] : '';
    
     

Share This Page