PHP.mk документација

parse_str

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.parse-str.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека function.parse-str.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
parse_str

Референца за `function.parse-str.php` со подобрена типографија и навигација.

function.parse-str.php

parse_str

(PHP 4, PHP 5, PHP 7, PHP 8)

parse_strПарсирај стринг како URL query стринг

= NULL

parse_str(string $string, array &$result): void

Парсира string како да е query стринг што е поминат преку URL и поставува клучеви во дадениот result низа. Ако не е result поминато, вредностите наместо тоа се поставуваат како променливи во тековниот опсег.

Параметри

string

, и враќа стринг со првиот карактер од

result

Променлива помината преку референца, која ќе биде поставена на низа што ги содржи паровите клуч-вредност извлечени од stringвредности (по редослед од најмалку до најмоќна) се валидни: result параметарот не е поминат, посебна променлива се поставува во локалниот опсег за секој клуч.

Ги ескејпува специјалните знаци во стринг за употреба во SQL изјава

Користењето на оваа функција без result параметарот е многу ОДЛОЖЕНО and DEPRECATED од PHP 7.2. Од PHP 8.0.0, result параметарот е mandatory.

Вратени вредности

Не се враќа вредност.

Дневник на промени

Верзија = NULL
8.0.0 result веќе не е опционално.
7.2.0 Употребата на parse_str() без втор параметар сега емитува E_DEPRECATED notice.

Примери

Пример #1 Користење parse_str()

<?php
$str
= "first=value&arr[]=foo+bar&arr[]=baz";

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

Сите празни места и точки во имињата на параметрите се претвораат во подвлекувања при креирање клучеви на низа или локални променливи. Ова е затоа што имињата на променливите во PHP не смеат да содржат празни места или точки, но се применува дури и кога се користи оваа функција со препорачаниот result parameter.

Пример #2 parse_str() именување

<?php
parse_str
("My Value=Something", $output);
echo
$output['My_Value']; // Something
?>

Белешки

Забелешка:

parse_str() е под влијание на max_input_vars директива. Надминувањето на овој лимит предизвикува E_WARNING, а сите променливи над лимитот не се додаваат во низата со резултати. Стандардно е 1000; прилагодете max_input_vars според потребата.

Забелешка:

Сите вредности пополнети во result низата (или променливите создадени ако вториот параметар не е поставен) се веќе URL-декодирани користејќи исти правила како urldecode().

Забелешка:

За да го добиете стринг од тековното барање, можете да ја користите променливата $_SERVER['QUERY_STRING']. Исто така, можеби ќе сакате да го прочитате делот за променливи од надворешни извори.

Види Исто така

  • parse_url() - Парсирај URL и врати ги неговите компоненти
  • pathinfo() - Враќа информации за патеката на датотеката
  • http_build_query() - Генерирај URL-кодиран стринг за прашање
  • urldecode() - Претвори ги сите применливи знаци во HTML ентитети

Белешки од корисници 32 белешки

Еван К
пред 18 години
It bears mentioning that the parse_str builtin does NOT process a query string in the CGI standard way, when it comes to duplicate fields.  If multiple fields of the same name exist in a query string, every other web processing language would read them into an array, but PHP silently overwrites them:

<?php
# silently fails to handle multiple values
parse_str('foo=1&foo=2&foo=3');

# the above produces:
$foo = array('foo' => '3');
?>

Instead, PHP uses a non-standards compliant practice of including brackets in fieldnames to achieve the same effect.

<?php
# bizarre php-specific behavior
parse_str('foo[]=1&foo[]=2&foo[]=3');

# the above produces:
$foo = array('foo' => array('1', '2', '3') );
?>

This can be confusing for anyone who's used to the CGI standard, so keep it in mind.  As an alternative, I use a "proper" querystring parser function:

<?php
function proper_parse_str($str) {
  # result array
  $arr = array();

  # split on outer delimiter
  $pairs = explode('&', $str);

  # loop through each pair
  foreach ($pairs as $i) {
    # split into name and value
    list($name,$value) = explode('=', $i, 2);
    
    # if name already exists
    if( isset($arr[$name]) ) {
      # stick multiple values into an array
      if( is_array($arr[$name]) ) {
        $arr[$name][] = $value;
      }
      else {
        $arr[$name] = array($arr[$name], $value);
      }
    }
    # otherwise, simply stick it in a scalar
    else {
      $arr[$name] = $value;
    }
  }

  # return result array
  return $arr;
}

$query = proper_parse_str($_SERVER['QUERY_STRING']);
?>
shagshag
пред 13 години
That's not says in the description but max_input_vars directive affects this function. If there are more input variables on the string than specified by this directive, an E_WARNING is issued, and further input variables are truncated from the request.
Ромер Ливарт
пред 4 години
If you need the key names preserved and don't want spaces or . or unmatched [ or ] replaced by an underscore, yet you want all the other goodies of parse_str(), like turning matched [] into array elements, you can use this code as a base for that.

<?php
const periodPlaceholder = 'QQleQPunT';
const spacePlaceholder = 'QQleQSpaTIE';

function parse_str_clean($querystr): array {
    // without the converting of spaces and dots etc to underscores.
    $qquerystr = str_ireplace(['.','%2E','+',' ','%20'], [periodPlaceholder,periodPlaceholder,spacePlaceholder,spacePlaceholder,spacePlaceholder], $querystr);
    $arr = null ; parse_str($qquerystr, $arr);

    sanitizeKeys($arr, $querystr);
    return $arr;
}

function sanitizeKeys(&$arr, $querystr) {
    foreach($arr as $key=>$val) {
        // restore values to original
        $newval = $val ;
        if ( is_string($val)) {
            $newval = str_replace([periodPlaceholder,spacePlaceholder], ["."," "], $val);
        }

        $newkey = str_replace([periodPlaceholder,spacePlaceholder], ["."," "], $key);
        
        if ( str_contains($newkey, '_') ) { 

            // periode of space or [ or ] converted to _. Restore with querystring
            $regex = '/&('.str_replace('_', '[ \.\[\]]', preg_quote($newkey, '/')).')=/';
            $matches = null ;
            if ( preg_match_all($regex, "&".urldecode($querystr), $matches) ) {

                if ( count(array_unique($matches[1])) === 1 && $key != $matches[1][0] ) {
                    $newkey = $matches[1][0] ;
                }
            }
        }
        if ( $newkey != $key ) {
            unset($arr[$key]);
            $arr[$newkey] = $newval ;
        } elseif( $val != $newval ) 
            $arr[$key] = $newval;

        if ( is_array($val)) {
            sanitizeKeys($arr[$newkey], $querystr);
        }
    }
}

?>

For example:

parse_str_clean("code.1=printr%28hahaha&code 1=448044&test.mijn%5B%5D%5B2%5D=test%20Roemer&test%5Bmijn=test%202e%20Roemer");

Produces:

array(4) {
  ["code.1"]=>
  string(13) "printr(hahaha"
  ["code 1"]=>
  string(6) "448044"
  ["test.mijn"]=>
  array(1) {
    [0]=>
    array(1) {
      [2]=>
      string(11) "test Roemer"
    }
  }
  ["test[mijn"]=>
  string(14) "test 2e Roemer"
}

Whereas if you feed the same querystring to parse_str, you will get

array(2) {
  ["code_1"]=>
  string(6) "448044"
  ["test_mijn"]=>
  string(14) "test 2e Roemer"
}
michael dot weibel at tilllate dot com
пред 10 години
If the arr argument is provided, all its existing elements are removed.
alxcube на gmail точка com
пред 9 години
if you need custom arg separator, you can use this function. it returns parsed  query as associative array.

<?php

/**
 * Parses http query string into an array
 * 
 * @author Alxcube <[email protected]>
 * 
 * @param string $queryString String to parse
 * @param string $argSeparator Query arguments separator
 * @param integer $decType Decoding type
 * @return array
 */
function http_parse_query($queryString, $argSeparator = '&', $decType = PHP_QUERY_RFC1738) {
        $result             = array();
        $parts              = explode($argSeparator, $queryString);

        foreach ($parts as $part) {
                list($paramName, $paramValue)   = explode('=', $part, 2);

                switch ($decType) {
                        case PHP_QUERY_RFC3986:
                                $paramName      = rawurldecode($paramName);
                                $paramValue     = rawurldecode($paramValue);
                                break;

                        case PHP_QUERY_RFC1738:
                        default:
                                $paramName      = urldecode($paramName);
                                $paramValue     = urldecode($paramValue);
                                break;
                }
                

                if (preg_match_all('/\[([^\]]*)\]/m', $paramName, $matches)) {
                        $paramName      = substr($paramName, 0, strpos($paramName, '['));
                        $keys           = array_merge(array($paramName), $matches[1]);
                } else {
                        $keys           = array($paramName);
                }
                
                $target         = &$result;
                
                foreach ($keys as $index) {
                        if ($index === '') {
                                if (isset($target)) {
                                        if (is_array($target)) {
                                                $intKeys        = array_filter(array_keys($target), 'is_int');
                                                $index  = count($intKeys) ? max($intKeys)+1 : 0;
                                        } else {
                                                $target = array($target);
                                                $index  = 1;
                                        }
                                } else {
                                        $target         = array();
                                        $index          = 0;
                                }
                        } elseif (isset($target[$index]) && !is_array($target[$index])) {
                                $target[$index] = array($target[$index]);
                        }

                        $target         = &$target[$index];
                }

                if (is_array($target)) {
                        $target[]   = $paramValue;
                } else {
                        $target     = $paramValue;
                }
        }

        return $result;
}

?>
php на voodoolabs точка net
19 години пред
This is probably a better solution than below. The first line makes sure the file doesn't exist then the second line directs all requests to a script. No need to output a 200 header with this method either.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php      [L]
kent на nospam точка ioflux точка com
20 години пред
You may want to parse the query string into an array. 

<?php
/**
 * Similar to parse_str. Returns false if the query string or URL is empty. Because we're not parsing to 
 * variables but to array key entries, this function will handle ?[]=1&[]=2 "correctly."
 *
 * @return array Similar to the $_GET formatting that PHP does automagically.
 * @param string $url A query string or URL 
 * @param boolean $qmark Find and strip out everything before the question mark in the string
*/
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;
}

/**
 * Utility function for parse_query_string. Given a result array, a starting key, and a set of keys formatted like "[a][b][c]" 
 * and the final value, updates the result array with the correct PHP array keys.
 *
 * @return void
 * @param array $result A result array to populate from the query string
 * @param string $k The starting key to populate in $result
 * @param string $arrayKeys The key list to parse in the form "[][a][what%20ever]"
 * @param string $value The value to place at the destination array key
*/
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;
    }
}

/**
* Breaks a string into a pair for a common parsing function. 
*
* The string passed in is truncated to the left half of the string pair, if any, and the right half, if anything, is returned.
*
* An example of using this would be:
* <code>
* $path = "Account.Balance";
* $field = string_pair($path);
* 
* $path is "Account"
* $field is "Balance"
*
* $path = "Account";
* $field = string_pair($path);
*
* $path is "Account"
* $field is false
* </code>
*
* @return string The "right" portion of the string is returned if the delimiter is found.
* @param string $a A string to break into a pair. The "left" portion of the string is returned here if the delimiter is found.
* @param string $delim The characters used to delimit a string pair
* @param mixed $default The value to return if the delimiter is not found in the string
* @desc 
*/
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;
}

?>
Тор Бјелсет
20 години пред
As of PHP 5, you can do the exact opposite with http_build_query(). Just remember to use the optional array output parameter.

This is a very useful combination if you want to re-use a search string url, but also slightly modify it:

Example:
<?
$url1 = "action=search&interest[]=sports&interest[]=music&sort=id";
$str = parse_str($url1, $output);

// Modifying criteria:
$output['sort'] = "interest";

$url2 = http_build_query($output);

echo "<br>url1: ".$url1;
echo "<br>url2: ".$url2;
?>

Results in:
url1: action=search&interest[]=sports&interest[]=music&sort=id
url2: action=search&interest[0]=sports&interest[1]=music&sort=interest

(Array indexes are automatically created.)
Оливие Менгуе
19 години пред
Vladimir: the function is OK in how it deals with &amp;.
&amp; must only be used when outputing URLs in HTML/XML data.
You should ask yourself why you have &amp; in your URL when you give it to parse_str.
tobsn на php точка net
пред 17 години
just a heads up with the example above:
?var[]=123 - the [] has to be urlencoded.
var names and var values - both have to be urlencoded!
avi на amarcus точка com
20 години пред
If you are trying to preserve a complex array, the function serialize might be better than http_build_query or other methods of making a query string.
helpmepro1 на gmail точка com
пред 17 години
<? 
//by shimon doodkin

 $url_form=url_to_form($url);
 echo '<form action="'.$url_form['action'].'" method="get">';
 echo $url_form['hidden'];
 echo '<input name="otherfiled" type="text">';
 echo '<input type="submit">';
 echo '</form>';

 function url_to_form($url)
 {
  $url=split('\?',$url,2);
  $action=$url[0];
  $hidden="";
  if(isset($url[1]))
  {
   $pairs=split('&',$url[1]);
   foreach($pairs as $pair)
   {
    $pair=split('=',$pair,2);
    $name=$pair[0];
    if(isset($pair[1]))
     $value=$pair[1];
    else
     $value='';
    $name=$name;
    $value=htmlspecialchars($value);
    if($name!='')
     $hidden.='<hidden name="'.$name.'" value="'.$value.'">';
   }
  }
  return array('action'=>$action,'hidden'=>$hidden);
 }

?>
StanE
пред 10 години
Note that the characters "." and " " (empty space) will be converted to "_". The characters "[" and "]" have special meaning: They represent arrays but there seems to be some weird behaviour, which I don't really understand:

<?php
// Note: "[" = %5B, "]" = %5D

/*
"v][=a" produces ("[" gets replaced by "_"):
Array
(
    [v]_] => a
)
*/
parse_str("v%5D%5B=a", $r);
print_r($r);

/*
"v][[=a" produces (first "[" gets replaced by "_", but not all following):
Array
(
    [v]_[] => a
)
*/
parse_str("v%5D%5B%5B=a", $r);
print_r($r);

?>
mike точка coley на inbox точка com
пред 18 години
Here is a little function that does the opposite of the parse_str function. It will take an array and build a query string from it.

<?php

/* Converts an array of parameters into a query string to be appended to a URL.
 *
 * @return  string              : Query string to append to a URL.
 * @param   array    $array     : Array of parameters to append to the query string.
 * @param   string   $parent    : This should be left blank (it is used internally by the function).
 */
function append_params($array, $parent='')
{
    $params = array();
    foreach ($array as $k => $v)
    {
        if (is_array($v))
            $params[] = append_params($v, (empty($parent) ? urlencode($k) : $parent . '[' . urlencode($k) . ']'));
        else
            $params[] = (!empty($parent) ? $parent . '[' . urlencode($k) . ']' : urlencode($k)) . '=' . urlencode($v);
    }

    $sessid = session_id();
    if (!empty($parent) || empty($sessid))
        return implode('&', $params);

    // Append the session ID to the query string if we have to.
    $sessname = session_name();
    if (ini_get('session.use_cookies'))
    {
        if (!ini_get('session.use_only_cookies') && (!isset($_COOKIE[$sessname]) || ($_COOKIE[$sessname] != $sessid)))
            $params[] = $sessname . '=' . urlencode($sessid);
    }
    elseif (!ini_get('session.use_only_cookies'))
        $params[] = $sessname . '=' . urlencode($sessid);

    return implode('&', $params);
}

?>

Note that the function will also append the session ID to the query string if it needs to be.
jrgns на jadeit точка co точка za
пред 13 години
The array to be populated does not need to be defined before calling the function:

<?php
error_reporting(E_ALL | E_STRICT);
parse_str('var=value', $array);
?>

This will not produce a notice.
chris на mcfadyen точка ca
пред 18 години
I shouldn't've posted the original version, as it only worked with the most basic of query strings.

This function will parse an html-safe query-like url string for variables and php-like ordered and associative arrays.  It places them into the global scope as parse_str does and adds minimal slashes for database insertions without the triple-slash problems that magic quotes can produce (the reason I had to write it in the first place).  If you don't need the slashes, they're easy enough to remove.

<?php
function parse_query($str) {
    
    // Separate all name-value pairs
    $pairs = explode('&', $str);
    
    foreach($pairs as $pair) {
        
        // Pull out the names and the values
        list($name, $value) = explode('=', $pair, 2);
        
        // Decode the variable name and look for arrays
        list($name, $index) = split('[][]', urldecode($name));
        
        // Arrays
        if(isset($index)) {
            
            // Declare or add to the global array defined by $name
            global $$name;
            if(!isset($$name)) $$name = array();
            
            // Associative array
            if($index != "") {
                ${$name}[$index] = addslashes(urldecode($value));
                
            // Ordered array
            } else {
                array_push($$name, addslashes(urldecode($value)));
            }
        
        // Variables
        } else {
            
            // Declare or overwrite the global variable defined by $name
            global $$name;
            $$name = addslashes(urldecode($value));
        }
    }
}
?>
PEPE_RIVAS на repixel точка net
19 години пред
CONVERT ANY FORMATTED STRING INTO VARIABLES

I developed a online payment solution for credit cards using a merchant, and this merchant returns me an answer of the state of the transaction like this:

estado=1,txnid=5555444-8454445-4455554,monto=100.00

to have all that data into variables could be fine for me! so i use str_replace(), the problem is this function recognizes each group of variables with the & character... and i have  comma separated values... so i replace comma with &

<?php
$string = "estado=1,txnid=5555444-8454445-4455554,monto=100.00";
$string = str_replace(",","&",$string);
parse_str($string);
echo $monto; // outputs 100.00
?>
jgbreezer на gmail точка com
19 години пред
Vladimir Kornea wrote on 8 Sep 2006:
"This function is confused by ampersands (&) being encoded as HTML entities (&amp;)"

Well, it would be - it's not supposed to be passed html entities, that's a different encoding scheme. This function does correctly decode url encoded params for you though (with the rawurlencode rather than urlencode, ie '+' is translated to a space).
chris на mcfadyen точка ca
пред 18 години
If you wish a version of parse_str sans magic quotes, the following will do the trick:

<?php
function parse_query($str) {
    $pairs = explode('&', $str);

    foreach($pairs as $pair) {
        list($name, $value) = explode('=', $pair, 2);
        global $$name;
        $$name = $value;
    }
}
?>
mortoray на ecircle-ag dot com
20 години пред
In Kent's solution you may wish to switch "urldecode" into "rawurldecode" if you'd like to get rid of the [annoying] plus '+' converted to space ' ' translation.
jab_creations на yahoo точка com
6 месеци пред
A simple though strict (and cleanly coded) version of the native function though without the pointless string replacement of periods with underscores:

<?php
function parse_query_string_mailto()
{//2025-09-11; Used in place of PHP's native parse_str() which pointlessly replaces periods with underscores:
 $r = false;

 $qs = ((isset($_SERVER['QUERY_STRING'])) ? urldecode($_SERVER['QUERY_STRING']) : false);

 if ($qs && strpos($qs, 'mailto:'))
 {
  $s = explode('mailto:', $qs, 2);

  if (count($s) == 2)
  {
   $r = array();
   $pairs = explode('&', $s[1]);

   foreach ($pairs as $pair)
   {
    $p = explode('=', $pair, 2);
    $r[$p[0]] = urldecode($p[1]);
   }
  }
 }

 return $r;
}
?>

Example relative URL:
mail/compose/?mailto%3Asubject%3DTest%2520Subject%26body%3DHello%2520World!%26from%3Duser%40example.com
Пример #1 Конвертирај WebP слика во jpeg слика користејќи
пред 4 години
Warning: `parse_str()` can cause "Input variables exceeded 1000" error (1000 is default php.ini setting for `max_input_vars`).

Test code.

<?php

$inputString = 'first=firstvalue';

for ($i = 1; $i <= 1100; $i++) {
    $inputString .= '&arrLoopNumber[]=' . $i;
}
unset($i);

echo 'input string: <code>' . $inputString . '</code><br>' . PHP_EOL;
echo '<h5>doing <code>parse_str()</code></h5>' . PHP_EOL;

$output = [];
parse_str($inputString, $output);
// errors!!
?>
twiddly
пред 8 години
proper_parse_str works great and I like that it doesn't replace spaces with underbars, but should urldecode $value
Вил Воелкер
пред 15 години
If you need a function that does something similar to parse_str, but doesn't convert spaces and dots to underscores, try something like the following:

<?php
function parseQueryString($str) {
    $op = array();
    $pairs = explode("&", $str);
    foreach ($pairs as $pair) {
        list($k, $v) = array_map("urldecode", explode("=", $pair));
        $op[$k] = $v;
    }
    return $op;
}
?>

It may need adapting to handle various edge cases.
motin на demomusic точка nu
19 години пред
When you have scripts run through the command-line (like locally via cron), you might want to be able to use _GET and _POST vars. Put this in top of your scheduled task files:

<?
    parse_str ($_SERVER['argv'][1], $GLOBALS['_GET']);
    parse_str ($_SERVER['argv'][2], $GLOBALS['_POST']);
?>

And call your script by:

/usr/local/bin/php /path/to/script.php "id=45&action=delete" "formsubmitted=true"

Cheers!
Михал Залевски
пред 18 години
Vladimir Kornea:
Try use html_entity_decode()

$str = 'first=value&amp;arr[]=foo+bar&amp;arr[]=baz';
parse_str(html_entity_decode($str), $output);
print_r($output);

Array
(
    [first] => value
    [arr] => Array
        (
            [0] => foo bar
            [1] => baz
        )

)
lenix.de
19 години пред
if you would like to get a nice url scheme with php/apache and and want to handle all requests in a central php script there's a simple solution/hack:

create a .htaccess in your "basedir" where you've got your main script (in this example index.php) containing some lines like:

"ErrorDocument 404 /index.php"

inside index.php you can now do

<?php
    $virtual_path = substr(
        $_SERVER['REQUEST_URI'],
        strlen( dirname( $_SERVER['PHP_SELF'] ) ) + 1
    );
    if( ($pos = strpos( $virtual_path, '?' )) !== false ) {
        parse_str( substr( $virtual_path, $pos + 1 ), $_GET );
        $_REQUEST = array_merge( $_REQUEST, $_GET );
        $virtual_path = substr( $virtual_path, 0, $pos );
    }

    // some code checking for a valid location, etc...
    header( 'HTTP/1.1 200 OK' );
    header( 'Content-Type: text/plain' );

    echo $virtual_path."\n\n";
    print_r( $_REQUEST );
?>

// guido 'lenix' boehm
kerosuppi
20 години пред
This does not work as expected.

<?php
class someclass
{
    var $query_string;
    function someclass($a_query_string)
    {
        $this->query_string = $a_query_string;
        parse_str($this->query_string);
    }
    function output()
    {
        echo $this->action;
    }
}

$a_class = new someclass("action=go");
$a_class->output();
?>

Use this instead.

<?php
class someclass
{
    var $arr;
    function someclass($a_query_string)
    {
        parse_str($a_query_string, $this->arr);
    }
    function output()
    {
        echo $this->arr['action'];
    }
}

$a_class = new someclass("action=go");
$a_class->output();
?>
markc
12 години пред
Beware using parse_str in a function that has vars passed by reference. It seems that parse_str actually creates new vars even if vars of the same name exist. If you pass by ref vars of the same name as those in a query string being parsed new LOCAL vers will be created and you won't get any values passed back to the caller (relates to what Maikel mentioned below)

An unrealistic example (vaguely related to what I was doing when I found this out)...

function get_title($query,&$title)
{
  parse_str($query);
  $title=str_replace("_"," ",$title);
}

$title="foo";
$query = "context=something&title=Title_of_Something";
get_title($query,$title);

echo $title .... "foo"
Владимир Корнеа
пред 18 години
parse_str() is confused by ampersands (&) being encoded as HTML entities (&amp;). This is relevant if you're extracting your query string from an HTML page (scraping). The solution is to run the string through html_entity_decode() before running it through parse_str().

(Editors: my original comment was a caution whose solution is obvious, but it has resulted in three replies ("so what?" "as intended" and "this is how to fix it"). Please remove the previous four posts dealing with this (69529, 70234, 72745, 74818) and leave just the above summary. This issue is too trivial to warrant the number of comments it has received.)
anatilmizun на gmail точка ком
21 години пред
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)){
            #strip comment
            $commt=strpos($ook,"##");
            if($commt!==false) $ook=substr($ook,0,$commt);
            #append
            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 ;)
Бенџамин Гарсија
пред 13 години
function like parse_str, but doesn't convert spaces and dots to underscores in $_GET AND $_POST

/**
 * GET and POST input containing dots, etc.
 */
function getRealREQUEST() {
    $vars = array();

    $input    = $_SERVER['REDIRECT_QUERY_STRING'];
    if(!empty($input)){
        $pairs    = explode("&", $input);
        foreach ($pairs     as $pair) {
            $nv                = explode("=", $pair);
            
            $name            = urldecode($nv[0]);
            $nameSanitize    = preg_replace('/([^\[]*)\[.*$/','$1',$name);
            
            $nameMatched    = str_replace('.','_',$nameSanitize);
            $nameMatched    = str_replace(' ','_',$nameMatched);
            
            $vars[$nameSanitize]    = $_REQUEST[$nameMatched];
        }
    }
    
    $input    = file_get_contents("php://input");
    if(!empty($input)){
        $pairs    = explode("&", $input);
        foreach ($pairs as $pair) {
            $nv                = explode("=", $pair);
            
            $name            = urldecode($nv[0]);
            $nameSanitize    = preg_replace('/([^\[]*)\[.*$/','$1',$name);
            
            $nameMatched    = str_replace('.','_',$nameSanitize);
            $nameMatched    = str_replace(' ','_',$nameMatched);
            
            $vars[$nameSanitize]    = $_REQUEST[$nameMatched];
        }
    }
    
    return $vars;
}
Навигација

Прелистувај сродни теми и функции.

На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.