PHP parse_​str() Function

What is PHP parse_str() Function?

If you want to parse a query string (the part after “?” of an URL) into an array, use PHP parse_str() function.

Syntax:

parse_str(string, array)

Parameters:

The function has 2 required parameters-

string (Required): It specifies the query string. Ex. In the URL “domain.com?name=john&age=30”, the query string is “name=john&age=30”.

result (Optional): It specifies an array where the key value pair of elements will be created from the query string.

Return Values:

The function returns nothing.

Examples:

Example 1:

<pre>
<?php
$query_string = "Name=John&Sex=Male";
parse_str($query_string, $array);
print_r($array);
?>
</pre>

Output:

Array
(
[Name] => John
[Sex] => Male
)

Notes on parse_str() Function:

  • Before version PHP 8.0.0, the 2nd parameter was optional. Then, if you omit it, the function will create variables directly in the local scope.
  • The function automatically decodes any encoded string from the query string using the same rules as urldecode(). For example, if an encoded string is “John%20Doe”, the function automatically converts it to “John Doe” and store in array.
  • The function converts dot (.) and space ( ) in the parameter name to underscore(_). Check example below.
    <pre>
    <?php
    $query_string = "First name=John&Last.name=Doe";
    parse_str($query_string, $array);
    print_r($array);
    ?>
    </pre>
    

    Output:

    Array
    (
    [First_name] => John
    [Last_name] => Doe
    )
  • Number of parameters in the query string can be controlled by the max_input_vars directive in the php.ini file.
  • If you use array in the query string, the function converts the array into nested array in the result. Check example below.
    <pre>
    <?php
    $query_string ="User[Name]=John&User[Sex]=Male&Level[]=Senior&Level[]=Junior";
    parse_str($query_string, $array);
    print_r($array);
    ?>
    </pre>
    

    Output:

    Array
    (
    [User] => Array
    (
    [Name] => John
    [Sex] => Male
    )
    [Level] => Array
    (
    [0] => Senior
    [1] => Junior
    )
    )
  • To extract the query part from an URL, you can use PHP_URL_QUERY constant with the parse_str() function. Check example below-
    <pre>
    <?php
    $url = "https://domain.com/page?Name=Jim&Age=35";
    $query_string = parse_url($url, PHP_URL_QUERY);
    parse_str($query_string, $array);
    print_r($array);
    ?>
    </pre>
    

    Output:

    Array
    (
    [Name] => Jim
    [Age] => 35
    )

Best Practices on parse_str() Function:

  • Validate and sanitize the parsed result.
  • To access $_GET, $_POST form data, use filter_input() function.
  • For safety, use urldecode() function though the parse_str() function automatically decode the query string.

Difference between parse_str() Function and parse_url() Function:

The parse_url() breaks an URL into parts and one part is “query string”. On the other hand, parse_str() function breaks a “query string” into key/value pairs.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP parse_str() Function

parse_str() is one of the frequently used string functions in PHP. Use this powerful function to parse a query string into an array.

Reference:

https://www.php.net/manual/en/function.parse-str.php