Format string in PHP

2

I saw some examples here in StackOverflow , but I just could not.

I need to format this string:

'personalization_id="v1_i8b1bAFy7m6K+j3TseNGDw=="'

If I use a $cookie = explode('=', $cookie); it looks like this:

0 => string 'personalization_id' (length=18)
1 => string '"v1_IQfbwu+xRVCTQaUk9sGfBQ' (length=26)
2 => string '' (length=0)
3 => string '"' (length=1)

Notice that the == in the end are gone, I need them to stay there, only = after the name of cookie sum.

What is the solution?

My code:

if (preg_match_all('/^Set-Cookie: \s*([^;]*)/mi', $response, $matches)) {
    foreach ($matches[1] as $cookie) {
        $cookie = explode('=', $cookie);
        var_dump($cookie);
    }
}

Edited

function getCSRF() {
    $request = curl_init();
    curl_setopt_array($request, [
            CURLOPT_URL                         => 'https://twitter.com/',
            CURLOPT_CUSTOMREQUEST       => 'GET',
            CURLOPT_RETURNTRANSFER  => true,
            CURLOPT_SSL_VERIFYHOST  => false,
            CURLOPT_SSL_VERIFYPEER  => false,
            CURLOPT_HEADER                  => true,
            CURLOPT_HTTPHEADER          => [
                'user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
            ]
        ]
    );
    $response = curl_exec($request);
    curl_close($request);

    if (preg_match_all('/^Set-Cookie: \s*([^;]*)/mi', $response, $matches)) {
        foreach ($matches[1] as $cookie) {
            $cookie = explode('=', $cookie);
            var_dump($cookie);
        }
    }
}
    
asked by anonymous 23.10.2017 / 18:55

1 answer

2

I do not know this form is correct, but I think my logic is correct, if it's good for me, then I think I answer my question:

if (preg_match_all('/^Set-Cookie: \s*([^;]*)/mi', $response, $matches)) {
        foreach ($matches[1] as $cookie) {
            $cookie = explode('=', $cookie);
            $key = (!empty($cookie[0])) ? $cookie[0] : '';
            $value = (!empty($cookie[1])) ? $cookie[1] : '';
            $value .= (isset($cookie[2])) ? '==' : '';
            $value .= (isset($cookie[3])) ? $cookie[3] : '';

            $obj->{$key} = $value;
            $obj->cookies .= $key . '=' . $obj->{$key} . '; '; 
        }
    }
    
23.10.2017 / 20:39