|
|
 |
parse_str (PHP 3, PHP 4, PHP 5) parse_str -- Parses the string into variables Descriptionvoid 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. If
the second parameter arr is present,
variables are stored in this variable as array elements instead.
Note:
Support for the optional second parameter was added in PHP 4.0.3.
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.
Example 1. Using parse_str() |
<?php
$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str);
echo $first; echo $arr[0]; echo $arr[1]; parse_str($str, $output);
echo $output['first']; echo $output['arr'][0]; echo $output['arr'][1]; ?>
|
|
See also parse_url(), pathinfo(),
get_magic_quotes_gpc(), and urldecode().
User Contributed Notes
parse_str
kent at nospam dot ioflux dot com
06-May-2005 10:13
You may want to parse the query string into an array.
<?php
function parse_query_string($url, $qmark=true)
{
if ($qmark) {
$pos = strpos($url, "?");
if ($pos !== false) {
$url = substr($url, $pos + 1);
}
}
if (empty($url))
return false;
$tokens = explode("&", $url);
$urlVars = array();
foreach ($tokens as $token) {
$value = string_pair($token, "=", "");
if (preg_match('/^([^\[]*)(\[.*\])$/', $token, $matches)) {
parse_query_string_array($urlVars, $matches[1], $matches[2], $value);
} else {
$urlVars[urldecode($token)] = urldecode($value);
}
}
return $urlVars;
}
function parse_query_string_array(&$result, $k, $arrayKeys, $value)
{
if (!preg_match_all('/\[([^\]]*)\]/', $arrayKeys, $matches))
return $value;
if (!isset($result[$k])) {
$result[urldecode($k)] = array();
}
$temp =& $result[$k];
$last = urldecode(array_pop($matches[1]));
foreach ($matches[1] as $k) {
$k = urldecode($k);
if ($k === "") {
$temp[] = array();
$temp =& $temp[count($temp)-1];
} else if (!isset($temp[$k])) {
$temp[$k] = array();
$temp =& $temp[$k];
}
}
if ($last === "") {
$temp[] = $value;
} else {
$temp[urldecode($last)] = $value;
}
}
function string_pair(&$a, $delim='.', $default=false)
{
$n = strpos($a, $delim);
if ($n === false)
return $default;
$result = substr($a, $n+strlen($delim));
$a = substr($a, 0, $n);
return $result;
}
?>
10-Mar-2005 06:26
yet another simpler way to do the reverse this function.
<?php
function build_str($query_array) {
$query_string = array();
foreach ($query_array as $k => $v) {
$query_string[] = $k.'='.$v;
}
return join('&', $query_string);
}
$test_query = "a=b&c=d";
parse_str($test_query, $query_array);
print_r($query_array);
if ( build_str($query_array) == $test_query ) {
echo "It works";
} else {
echo "It doesn't work";
}
?>
nospam at fiderallalla dot de
03-Feb-2005 04:29
Maybe you need an opposite which works with arrays:
<?php
function query_str ($params) {
if ( !is_array($params) || count($params) == 0 ) return false;
$fga = func_get_args();
$akey = ( !isset($fga[1]) ) ? false : $fga[1];
static $out = Array();
foreach ( $params as $key=>$val ) {
if ( is_array($val) ) {
query_str($val,$key);
continue;
}
$thekey = ( !$akey ) ? $key : $akey.'['.$key.']';
$out[] = $thekey."=".$val;
}
return implode("&",$out);
}
?>
anatilmizun at gmail dot com
12-Oct-2004 06:10
I wrote a pair of functions using parse_str() that will write values in an array to a textfile and vice versa, read those values from the textfile back into the array. Quite useful if you need to store lots of data but don't have access to SQL.
Save the array by calling cfg_save($filename,$array) and load it back using $array=cfg_load($filename)
<?php
$newline="¤";
function cfg_load($cfgfile){
global $newline;
$setting="";
if(file_exists($cfgfile)){
$setting=fopen($cfgfile, "r");
$ookk="";
while($ook=fgets($setting)){
$commt=strpos($ook,"##");
if($commt!==false) $ook=substr($ook,0,$commt);
if($ook!="") $ookk=$ookk."&". str_replace($newline,"\n",str_replace("&","%26",trim($ook)));
}
fclose($setting);
parse_str($ookk, $setting);
}
return $setting;
}
function cfg_save($cfgfile,$setting){
global $intArray;
$intArray="";
for($i=0;$i<2000;$i++)
$intArray[]=$i;
if(is_array($setting)){
$allkeys=array_keys($setting);
foreach($allkeys as $aKey)
cfg_recurse($setting[$aKey], $aKey, $outArray);
}
$cfgf=fopen($cfgfile,"w");
foreach($outArray as $aLine)
fputs($cfgf,stripslashes($aLine)."\r\n");
fclose($cfgf);
}
function cfg_recurse($stuffIn, $keysofar, &$toAppend){
global $intArray, $newline;
if(is_array($stuffIn)){
$allkeys=array_keys($stuffIn);
if(array_slice($intArray,0,sizeof($allkeys))==$allkeys)
$nokey=true;
else
$nokey=false;
foreach($allkeys as $aKey){
if(!$nokey) $toKey=$aKey;
cfg_recurse($stuffIn[$aKey], $keysofar."[".$toKey."]", $toAppend);
}
}else
$toAppend[]=$keysofar."=".str_replace("\n",$newline,$stuffIn);
}
?>
Note that these functions support nested arrays of unlimited levels ;)
Matt Curtis
08-Sep-2004 10:46
If the querystring contains duplicate keys in the key-value pairs, parse_str will only return the last instance of the value. For example, in the following:
<?php
$mystr = "test1=blah&test2=bleh&test1=burp";
parse_str($mystr, $myarray);
echo $myarray['test1'];
?>
The value output will be 'burp'.
I wrote a function that takes a querystring and returns the the key-value pairs as a two-dimensional array so each duplicate key is available:
<?php
$str = "test1=blah&test2=bleh&test1=burp";
$valsarray = parse_str_ext($str);
echo $valsarray['test1'][0];
echo $valsarray['test1'][1];
echo $valsarray['test2'][0];
function parse_str_ext($toparse) {
$returnarray = array();
$keyvaluepairs = split("&", $toparse);
foreach($keyvaluepairs as $pairval) {
$splitpair = split("=", $pairval);
if(!array_key_exists($splitpair[0], $returnarray)) $returnarray[$splitpair[0]] = array();
$returnarray[$splitpair[0]][] = $splitpair[1];
}
return $returnarray;
}
?>
Output will be:
blah
burp
bleh
17-Jun-2004 07:23
Note that variables cannot contain a DOT (.) in PHP. So, DOT will be replaced by underscore.
e.g. variables like "variable.something" will be converted into "variable_something".
31-Mar-2004 12:58
The documentation does not appear to mention that parse_str also urldecodes each item in the resulting array.
There also appears to be a bug in earlier versions of PHP that causes these urldecoded strings to also be escaped. (Certainly I was having problems with %22 being turned into /" on my server, but not on my development box, despite forcing magic quotes off).
dante at lorenso dot com
06-Jul-2003 12:11
You can perform the opposite of this function if you like with a function like I've built below:
/**
* Reverse of parse_str(). Converts array into
* string with query format
*/
function query_str ($params) {
$str = '';
foreach ($params as $key => $value) {
$str .= (strlen($str) < 1) ? '' : '&';
$str .= $key . '=' . rawurlencode($value);
}
return ($str);
}
-- Dante
| |