Home  >  Article  >  Backend Development  >  How to use parse_str in PHP

How to use parse_str in PHP

PHPz
PHPzOriginal
2023-09-04 10:01:051832browse

In this article, we will explore the parse_str function in PHP. It is used to convert the GET request query string into usable variables.

In day-to-day PHP development, you often need to deal with query strings, which is how data is passed to a script in a URL via a GET request. When passing query string in URL, PHP provides super global $_GET variable which contains all query parameters as PHP variables, so you can easily read the parameters available in query string. However, sometimes you need to process the raw query string and convert it into a variable.

In PHP, there are a few different ways to accomplish this. You can use the explode function to split the query string by & characters and then by = characters. But there's an easier way: PHP provides the parse_str function, which allows you to parse the query string in a single call.

Today, we will discuss the parse_str function in detail, as well as a few practical examples.

Syntax: parse_str Function

Let’s take a look at the syntax of the parse_str function:

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

As you can see, parse_str has two parameters. The first parameter is the input string, which should be in query string format, e.g. name=Wilson&color=blue.

The second parameter is an array that will be populated by the variables parsed in the query string.

It is important to note that the parse_str function does not return anything, so you must use the second parameter to initialize the result of the function. Prior to PHP 7.2, the second argument was optional, omitting it told parse_str to return the resulting value as a variable in local scope, but starting with PHP 7.2, this is a mandatory argument.

Additionally, the parse_str function always decodes URL-encoded variables, so you do not need to use the urldecode function. In addition to this, if you have a long input query string, it may throw input variables exceeds X error. In this case, check the max_input_vars configuration value in the php.ini configuration file and adjust it according to your requirements.

  • How to use parse_str in PHP

让我们在下一节中看几个现实世界的示例。

parse_str

在本节中,我们将通过几个实际示例来演示如何使用 parse_str 函数。

一个简单的例子

让我们从一个简单的示例开始,如以下代码片段所示。

<?php
$string = 'first_name=John&last_name=Richards&age=30';
parse_str($string, $result);
print_r($result);

/**
Output:
Array
(
    [first_name] => John
    [last_name] => Richards
    [age] => 30
)
**/

如您所见, parse_str 函数解析 $string 查询字符串,并且 $result 填充有一个数组。变量名称被转换为数组键,变量值被分配给相应的数组键。

数组示例

很多时候,输入的查询字符串可能包含数组变量,parse_str函数可以检测到它并将其转换为相应的数组变量。

<?php
$string = 'foo=1&bar[]=1&bar[]=2';
parse_str($string, $result);
print_r($result);
/**
$result Output:
Array
(
    [foo] => 1
    [bar] => Array
        (
            [0] => 1
            [1] => 2
        )

)
**/

正如您在上面的示例中看到的,bar 变量在输入查询字符串中包含多个值,并且它被转换为相应的 $result['bar'] 数组,如输出所示。

请注意,如果您在数组变量后不使用特殊的 [] 语法,则 parse_str 会将其视为常规变量,并且仅保留字符串中的最后一个值。这与大多数查询字符串解析器的工作方式略有不同。

特殊字符示例

有时,输入查询字符串包含空格和点作为变量名称的一部分。但是,PHP 不允许变量名中包含空格和点,因此它们会被 parse_str 函数自动转换为下划线。

让我们看一下下面的示例来了解它是如何工作的。

<?php
$string = 'my name=John&my.email=john@example.com';
parse_str($string, $result);
print_r($result);
/**
$result Output:
Array
(
    [my_name] => John
    [my_email] => john@example.com
)
**/

如果您注意到,输入查询字符串变量中的空格和点字符将替换为数组键中的下划线 (_) 字符。

URL 编码值示例

正如我们之前讨论的,parse_str 函数在解析期间始终对 URL 编码值进行解码。这意味着您不需要单独应用 urldecode 函数。

这是一个包含一些 URL 编码值的示例。

<?php
$string = 'name=John%20Richards&profile_url=http%3A%2F%2Fexample.com%2Fmyprofile';
parse_str($string, $result);
print_r($result);
/**
$result Output:
Array
(
    [name] => John Richards
    [profile_url] => https://example.com/myprofile
)
**/

如您所见,URL 编码的字符会自动转换为相应的字符。

结论

今天,我们讨论了 PHP 中的 parse_str 函数,该函数对于将查询字符串转换为变量非常有用。我希望您发现它对您自己的 PHP 编码很有用!

The above is the detailed content of How to use parse_str in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn