parse_str

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

parse_strParses the string into variables

Descripción

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

Parses string as if it were the query string passed via a URL and sets variables in the current scope (or in the array if result is provided).

Parámetros

string

The input string.

result

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

Advertencia

Using this function without the result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2. As of PHP 8.0.0, the result parameter is mandatory.

Valores devueltos

No devuelve ningún valor.

Historial de cambios

Versión Descripción
8.0.0 result is no longer optional.
7.2.0 Usage of parse_str() without a second parameter now emits an E_DEPRECATED notice.

Ejemplos

Ejemplo #1 Using parse_str()

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

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

// DISCOURAGED
parse_str($str);
echo
$first; // value
echo $arr[0]; // foo bar
echo $arr[1]; // baz
?>

Because variables in PHP can't have dots and spaces in their names, those are converted to underscores. Same applies to naming of respective key names in case of using this function with result parameter.

Ejemplo #2 parse_str() name mangling

<?php
parse_str
("My Value=Something");
echo
$My_Value; // Something

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

Notas

Nota:

parse_str()() is affected by the max_input_vars directive. Exceeding this limit triggers an E_WARNING, and any variables beyond the limit are not added to the result array. The default is 1000; adjust max_input_vars as needed.

Nota:

All variables created (or values returned into array if second parameter is set) are already urldecode()d.

Nota:

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.

Ver también

  • parse_url() - Analiza un URL y devuelve sus componentes
  • pathinfo() - Devuelve información acerca de la ruta de un fichero
  • http_build_query() - Generar una cadena de consulta codificada estilo URL
  • urldecode() - Decodifica una cadena cifrada como URL