parse_​str – PHP String Functions

Syntax :

parse_​str ( stringto_encode, array );

Description :

The parse_​str() function parses a query string into variables. Parses stringto_encode, as if it were the query string passed via a URL and sets variables in the current scope.

Warning : Using it without result parameter is highly DISCOURAGED and DEPRECATED as of PHP 7.2.

Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|lef|lef|left”]
Name, Required /Optional, Value Type, Description
stringto_encode , Required, String , It is the string for which is to be parsed.
array, Optional, Array, It tells the name of array in which values will be stored.
[/table]


Output :

This will return nothing.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 7.2.0 , Usage of parse_str() without a second parameter now emits an E_DEPRECATED notice.
[/table]


Related articles : chr()crc32()md5_file()crypt(), htmlspecialchars_decode().


parse_​str() – PHP Functions Example 1 :
<?php 
$strExample = "firstvar=val1&arr[]=tuts+mines&arr[]=net"; 

// Recommended
parse_str($strExample, $output);
echo "\n".$output['firstvar'];  // val1
echo "\n".$output['arr'][0]; // tuts mines
echo "\n".$output['arr'][1]; // net 
 
// DISCOURAGED
parse_str($strExample);
echo "\n".$firstvar;  // value
echo "\n".$arr[0]; // tuts mines
echo "\n".$arr[1]; // net
?>

See below is the output of above code in Web browser.

val1
tuts mines
net
val1
tuts mines
net

parse_​str() – PHP Functions Example 2 : name mangling
<?php 
parse_str("my var=val1");
echo $my_var."\n";  
parse_str("my var=val1", $output);
echo $output['my_var']; // Something
?>

See below is the output of above code in Web browser.

val1
val1

Notes :

[table caption=”” width=”100%” colwidth=”100%” colalign=”left|left”]
All variables created are already urldecoded().
We can use $_SERVER[‘QUERY_STRING’] to get the query string variables.
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 and $_POST etc. variables.
Without array param variables set by this function will overwrite existing variables of the same name..
[/table]


 

You may also like...