Search code examples
phpvariablesparse-url

PHP parse_str not getting all $_GET variables


I have a strange issue. Would like to fetch the $_GET variables from a string with parse_str. Which works except for the first variable.

Makes me wonder if I have written the the syntax correctly. But yes I think I (probably) did.

When I run the following 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

Through the following code :

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

I receive the following variables :

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
)

It makes me suggest that my syntax is incorrect. system/filter/index?filter

But then is my question. How to format it then? I thought this should work. Whenever I set : index.php?filter it makes no difference in results.

UPDATE

Added the solution as posted by BA_Webimax

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

And it works as a charm!

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
)

Solution

  • parse_str() was meant to work on the Query String and not a full URL. In order to properly separate the URL components, I recommend that you use parse_url() first to isolate the query string.

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