How to extract specific variable in url string?

Discussion in 'PHP' started by peppy, Jul 9, 2010.

  1. #1
    I have a random string like this with variables separated with "&":

    shipping=free&cost=100&currency=usd&status=pending&time=...&..&...&.....

    I would like PHP to search through a random string for "&status=" to extract store corresponding result into a variable: $status

    How can this be done?

    Thanks
     
    peppy, Jul 9, 2010 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Is this a link that has been clicked? If so it will be in $_GET - just reference the array.

    If you are scraping someone else's page and want the info the easiest way is just to explode the url and then work through the rows until you get to status.
     
    sarahk, Jul 9, 2010 IP
  3. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $status = $_GET['status']

    SANITIZE
     
    krsix, Jul 10, 2010 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    By that krsix means check that the contents of $status are what you are expecting. $_GET and $_POST variables can be manipulated to hack your database. Don't get caught out.
     
    sarahk, Jul 10, 2010 IP
  5. sickness01

    sickness01 Member

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    yes the different variables in shipping=free&cost=100&currency=usd&status=pending
    $shipping = $_GET['shipping']
    $cost = $_GET['cost']
    $currency = $_GET['currency']
    $status = $_GET['status']

    the variable is the first part, before the = , shipping, cost, currency, status. the the result is the second part, after the =

    so $shipping = $_GET['shipping'] would be $shipping = 'free', hope that helps you out a bit
     
    sickness01, Jul 11, 2010 IP
  6. raredaredevil

    raredaredevil Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    
    $targetstr="shipping=free&cost=100&currency=usd&status=pending&time=";
    $finalstr =  GetStringBetween($targetstr,"&status=","&");
    print $finalstr;
    
        private function GetStringBetween($String, $Start, $End){
                if($String != "" && $Start != "" && $End != ""){
                    $Pos_1 = strpos($String, $Start);
                    $Pos_2 = strpos($String, $End, ($Pos_1 + 1));
                    
                    if(is_numeric($Pos_1) && is_numeric($Pos_2)){
                        $Between = substr($String, ($Pos_1 + strlen($Start)), ($Pos_2 - $Pos_1 - strlen($Start)));
                        
                        if(strlen($Between) > 0){
                            return $Between;
                        }
                    }
                }
                
                return false;
            }  
    PHP:
    This will print the status value :) in this case "pending"
     
    raredaredevil, Jul 11, 2010 IP