I have a random string like this with variables separated with "&": shipping=free&cost=100¤cy=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
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.
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.
yes the different variables in shipping=free&cost=100¤cy=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
$targetstr="shipping=free&cost=100¤cy=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"