1

Is there a way to parse this querystring?

$output = 'Country=UNITED STATES (US) &City=Scottsdale, AZ &Latitude=33.686 &Longitude=-111.87';

Ultimately, I want to isolate the latitude and longitude values in to separate variables, strtok() is not serving the purpose.

7 Answers 7

8

You don't need a regular expression for this; use explode() to split up the string, first by &, and then by =, which you can use to effectively parse it into a nice little array mapping names to values.

Sign up to request clarification or add additional context in comments.

4 Comments

could you please give an example of the above? Thanks.
$var_array = explode('&', $var_string); foreach($var_array as $var_to_split)
I messed up that last entry by hitting enter. Not a fan of that style of auto-submit. And now it doesn't seem to wrap the following in a pre-code block no matter what I try ` // Separate the strings into individual vars $var_array = explode('&', $var_string); $mapped_var_array = array(); // Set up this to hold mapped vars foreach($var_array as $var_to_split){ $var_to_split = explode('=', $var_to_split); $mapped_var_array[$var_to_split[0]] = $var_to_split[1]; } // Now do whatever you want with $mapped_var_array ` Something like that. @hitautodestruct
Thanks alot @prayfomojo. I also added your comment to the answer.
3
$output='Country=UNITED STATES (US) &City=Scottsdale, AZ &Latitude=33.686 &Longitude=-111.87';
parse_str($output, $array);
$latitude = $array['latitude'];

You could also just do

parse_str($output);
echo $latitude;

I think using an array is better as you are not creating variables all over the place, which could potentially be dangerous (like register_globals) if you don't trust the input string.

Comments

2

It looks likes it's coming from an URL, although the URL encoding is gone.

I second the suggestions of using explode() or preg_split() but you might also be interested in parse_str().

$output = "City=Scottsdale, AZ &Latitude=33.686 &Longitude=-111.87";
parse_str($output, $results);
$lat = $results['Latitude'];
$lon = $results['Longitude'];

Comments

0

I'm not sure exactly what you're demonstrating in your code, maybe it's just some typos, is the "$output" the name of the variable, or part of the string?

Assuming it's the name of the variable, and you just forgot to put the quotes on the string, you have a couple options:


$sliced = explode('&', $output)

This will create an array with values: "Country=UNITED STATES(US) ", "City=Scottsdale, AZ ", etc.


$sliced = preg_split('/[&=]/', $output);

This will create an array with alternating elements being the "variables" and their values: "Country", "UNITED STATES(US) ", "City", "Scottsdale, AZ ", etc.

Comments

0

You could do the following:

$output = 'Country=UNITED STATES (US) &City=Scottsdale, AZ &Latitude=33.686 &Longitude=-111.87';

$strs = explode('&', $output);
foreach ($strs as $str) {

   list($var, $val) = explode('=',$str);
   $$var = trim($val);

}

This would give you variables such as $Latitude and $Longitude that are set to the value in the key/value pair.

2 Comments

Variable variables? Please no :(
This is a really bad idea. What if your keys contain spaces? PHP's associative arrays make more sense here: $array[$var] = trim($val);
0

All of the other Answers are Insufficient. This Should Work Every Time!

function slice($string, $start){
  $st = $string; $s = $start; $l = strlen($st); $a = func_get_args();
  $e = isset($a[2]) ? $a[2] : $l;
  $f = $e-$s;
  $p = $e < 0 && $s > 0 ? $l+$f : $f;
  return substr($st, $s, $p);
}

Comments

-1

I suggest you read about regular expressions in the PHP help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.