0

我有一个奇怪的问题。想使用parse_str从字符串中获取 $_GET 变量。除第一个变量外,哪个有效。

让我想知道我是否正确编写了语法。但是是的,我认为我(可能)做到了。

当我运行以下 URL 时:

system/filter/index?filter[date_selecttype]=date&filter[period_start]=08-08-2019&filter[period_end]=31-08-2019&filter[set_date]=30-08-2019&filter[booking_select_type]=booked&filter[booking_panel][6] =on&filter[booking_panel][9]=on&filter[booking_panel][1]=on&filter[booking_panel][2]=on&filter[booking_panel][11]=on&filter[booking_panel][4]=on&filter[booking_panel][5]=on&filter [booking_panel][10]=on&filter[booking_panel][7]=on&filter[booking_panel][3]=on&filter[booking_panel][12]=on&filter[booking_panel][8]=on&filter[booking_state][1]=on&filter[booking_state ][0]=on&filter[booking_state][2]=on&filter[booking_state][3]=on&filter[payment_state]=&filter[select_paymentmethod]=&filter[booking_source]=&filter[booking_saleschannel]=&filter[booking_travelcode]=&filter[booking_discountcode]=&execute_filter=1

通过以下代码:

        $UrlStr = urldecode(str_replace('&', '&', __FULLURL__));
        parse_str($UrlStr, $GETVariables);

我收到以下变量:

Array
(
    [system/filter/index?filter] => Array
        (
            [date_selecttype] => date
        )

    [filter] => Array
        (
            [period_start] => 08-08-2019
            [period_end] => 31-08-2019
            [set_date] => 30-08-2019
            [booking_select_type] => booked
            [booking_panel] => Array
                (
                    [6] => on
                    [9] => on
                    [1] => on
                    [2] => on
                    [11] => on
                    [4] => on
                    [5] => on
                    [10] => on
                    [7] => on
                    [3] => on
                    [12] => on
                    [8] => on
                )

            [booking_state] => Array
                (
                    [1] => on
                    [0] => on
                    [2] => on
                    [3] => on
                )

            [payment_state] => 
            [select_paymentmethod] => 
            [booking_source] => 
            [booking_saleschannel] => 
            [booking_travelcode] => 
            [booking_discountcode] => 
        )

    [execute_filter] => 1
)

这让我认为我的语法不正确。 系统/过滤器/索引?过滤器

但这是我的问题。那怎么格式化呢?我认为这应该可行。每当我设置 : index.php?filter它对结果没有影响。

更新

添加了BA_Webimax发布的解决方案

    $UrlStr         = urldecode( str_replace( '&', '&', __FULLURL__ ) );
    $query_string   = parse_url($UrlStr, PHP_URL_QUERY );
    parse_str($query_string, $GETVariables );

它是一种魅力!

Array
(
    [filter] => Array
        (
            [date_selecttype] => date
            [period_start] => 08-08-2019
            [period_end] => 31-08-2019
            [set_date] => 30-08-2019
            [booking_select_type] => booked
            [booking_panel] => Array
                (
                    [6] => on
                    [9] => on
                    [1] => on
                    [2] => on
                    [11] => on
                    [4] => on
                    [5] => on
                    [10] => on
                    [7] => on
                    [3] => on
                    [12] => on
                    [8] => on
                )

            [booking_state] => Array
                (
                    [1] => on
                    [0] => on
                    [2] => on
                    [3] => on
                )

            [payment_state] => 
            [select_paymentmethod] => 
            [booking_source] => 
            [booking_saleschannel] => 
            [booking_travelcode] => 
            [booking_discountcode] => 
        )

    [execute_filter] => 1
)
4

1 回答 1

2

parse_str()旨在处理查询字符串而不是完整的 URL。为了正确分离 URL 组件,我建议您parse_url()首先使用来隔离查询字符串。

$UrlStr = urldecode( str_replace( '&', '&', __FULLURL__ ) );

$query_string = parse_url( $UrlStr, PHP_URL_QUERY );

parse_str( $query_string, $GETVariables );
于 2019-08-16T16:33:29.430 回答