PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




parse_str

Name

parse_str — Parses a query string into variables.

Synopsis

void parse_str(query_string[, $array]);
string query_string: String to parse into variables
variable $array (optional): Array in which to place the variables

Returns

Nothing

Description

parse_str() parses a query string (such as ?id=10&name=Ziggy%20Stardust") into variables that are then set in the local scope. If the optional $array argument is set, the variables are stored in $array as an array instead of being set in the local scope.

Note

PHP automatically handles GET and POST data. In most cases, there is no need to decode query strings with this function.



Note

If the magic_quotes_gpc configuration directive is enabled, the variables are processed with addslashes() before they're set.



Caution

If $array is not set, variables parsed out of the query_string overwrite variables that already exist in the local scope.



Version

PHP 3+, PHP 4+ (the argument was added in PHP 4.0.3)

See also

To break a URL into separate components such as host, protocol, etc.:

parse_url()

To control how/whether PHP imports GET/POST/COOKIE data:

The gpc_order, register_globals, and track_vars configuration directives



Example

Example 1211. Demonstrate how parse_str() overwrites variables

<?php
$query_string = "PHP_SELF=oops";

echo <<<_EOS_
Before parsing the variable out of '$query_string' with parse_str(),
\$PHP_SELF contained '$PHP_SELF'\n\n
_EOS_;

parse_str($query_string);

echo <<<_EOS_
After parsing the variable out of '$query_string' with parse_str(),
\$PHP_SELF contains '$PHP_SELF'
_EOS_;
?>

Sample output:
Before parsing the variable out of 'PHP_SELF=oops' with parse_str(),
$PHP_SELF contained '/test/test.php'

After parsing the variable out of 'PHP_SELF=oops' with parse_str(),
$PHP_SELF contains 'oops'

Example 1212. Extract the variables from a stored query string

<?php
$query_string = "?id=acbd18db4cc2f85cedef654fccc4a4d8&i=F4&s=3";
parse_str($query_string, $output);
var_dump($output);
?>
Output:
array(3) {
  ["id"]=>
  string(32) "acbd18db4cc2f85cedef654fccc4a4d8"
  ["i"]=>
  string(2) "F4"
  ["s"]=>
  string(1) "3"
}



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.