Format string in PHP

Asked

Viewed 269 times

0

I saw some examples here on Stackoverflow, but I just couldn’t.

I need to format this string:

'personalization_id="v1_i8b1bAFy7m6K+j3TseNGDw=="'

If I use one $cookie = explode('=', $cookie); she gets 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 gone, I need them to stay there, only = after the name of the cookie scram.

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);
        }
    }
}
  • It can’t just be '$cookie = explode('="', $cookie);' ?

  • No he returns me only one = at the end and the remains of cookies will not be a array.

  • Are several cookies in the string ? they will always be the same ?

  • Yes they are various with different keys and values, another example: fm=0

  • 1

    But the names of the cookies will be the same correct ? I don’t think it can explode, I think you’ll have to use preg_math one for each cookie, but it will only work if the names are always

  • Aaa has to have a way. twid="u=123456789" this also removes = after the u

  • I edited the question the last code just run.

Show 2 more comments

1 answer

0


I do not know is correct this way, but I think my logic is correct, if it is good for me, then I believe 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} . '; '; 
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.