PYTHON parse_str

is this article helpful?
|
Python replacement for PHP's parse_str [ edit | history ]
s = 'first=value&sec=foo+bar'
#2.7
import urlparse
print urlparse.parse_qs(s) # {'first': ['value'], 'sec': ['foo bar']}
# 2.6
import urlparse
print urlparse.parse_qs(s) # {'first': 'value', 'sec': 'foo bar'}

# 3.x
import urllib.parse
print urllib.parse.parse_qs(s) # {'first': 'value', 'sec': 'foo bar'}

# < 2.6
import cgi
print cgi.parse_qs(s) # {'first': 'value', 'sec': 'foo bar'}

PHP parse_str

PHP original manual for parse_str [ show | php.net ]

parse_str

(PHP 4, PHP 5)

parse_strParses the string into variables

Description

void parse_str ( string $str [, array &$arr ] )

Parses str as if it were the query string passed via a URL and sets variables in the current scope.

Note: To get the current QUERY_STRING, you may use the variable $_SERVER['QUERY_STRING']. Also, you may want to read the section on variables from external sources.

Note: The magic_quotes_gpc setting affects the output of this function, as parse_str() uses the same mechanism that PHP uses to populate the $_GET, $_POST, etc. variables.

Parameters

str

The input string.

arr

If the second parameter arr is present, variables are stored in this variable as array elements instead.

Return Values

No value is returned.

Changelog

Version Description
4.0.3 The arr parameter was added

Examples

Example #1 Using parse_str()

<?php
$str 
"first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo 
$first;  // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz

parse_str($str$output);
echo 
$output['first'];  // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

?>

See Also