0

Hi Can anyone please help me in parsing the below string in PHP and make it as array

The value are seperated by ;

String Value

HTTP/1.1 200 OK
Set-Cookie: LWSSO_COOKIE_KEY=UUo07jYJvrMtRg_GbKTV5Tf2hmI6N4XQRyVR7lLl9M6PphiPdmSWwLU7Pr1ZMRra10gv4HGLw9PlKDvyhPD0Z23u-Mv2S8zXf5Jgt67JGOV06J7d3I_ne7KmbbhwBIpvisedcBhBDLXfrOP-aFvJvdYekAtikN0GAU4Q2eTt1GWxPOJrRQUS2sSWXKC0A5NKUszUwkg11oBe-sXhsLmy5NiSDSErGNt_XBowWA-AgT2CFlB3J-xpUQ1xupJ8rs4H3A1u8zIGtlIWKtawLQLaKQYhQwr8DXnJUxpt0YNiyEg.;Path=/;HTTPOnly
Set-Cookie: QCSession=NzE1ODE1MjtlYlZOalo2eGNwNkFyOXdpVE52Q0RRKio7UkVTVCBjbGllbnQ7IDsg;Path=/;HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: ALM_USER=b3a903d0544b5aaa1f150e2b8f6ad420b4e8ad33dea552dacd02479c4f07fe41;Path=/
Set-Cookie: XSRF-TOKEN=ad3d73000606ca7bf6aa5773344720f7bbe0201c154e51c8a770a0bb29e0db38;Path=/
Content-Length: 0
Server: Jetty(9.1.4.v20140401)

Now I want the array with below values

[Set-Cookie: LWSSO_COOKIE_KEY] => UUo07jYJvrMtRg_GbKTV5Tf2hmI6N4XQRyVR7lLl9M6PphiPdmSWwLU7Pr1ZMRra10gv4HGLw9PlKDvyhPD0Z23u-Mv2S8zXf5Jgt67JGOV06J7d3I_ne7KmbbhwBIpvisedcBhBDLXfrOP-aFvJvdYekAtikN0GAU4Q2eTt1GWxPOJrRQUS2sSWXKC0A5NKUszUwkg11oBe-sXhsLmy5NiSDSErGNt_XBowWA-AgT2CFlB3J-xpUQ1xupJ8rs4H3A1u8zIGtlIWKtawLQLaKQYhQwr8DXnJUxpt0YNiyEg.

[Set-Cookie: QCSession] => NzE1ODE1MjtlYlZOalo2eGNwNkFyOXdpVE52Q0RRKio7UkVTVCBjbGllbnQ7IDsg

I have tried below code

$response = 'HTTP/1.1 200 OK
    Set-Cookie: LWSSO_COOKIE_KEY=UUo07jYJvrMtRg_GbKTV5Tf2hmI6N4XQRyVR7lLl9M6PphiPdmSWwLU7Pr1ZMRra10gv4HGLw9PlKDvyhPD0Z23u-Mv2S8zXf5Jgt67JGOV06J7d3I_ne7KmbbhwBIpvisedcBhBDLXfrOP-aFvJvdYekAtikN0GAU4Q2eTt1GWxPOJrRQUS2sSWXKC0A5NKUszUwkg11oBe-sXhsLmy5NiSDSErGNt_XBowWA-AgT2CFlB3J-xpUQ1xupJ8rs4H3A1u8zIGtlIWKtawLQLaKQYhQwr8DXnJUxpt0YNiyEg.;Path=/;HTTPOnly
    Set-Cookie: QCSession=NzE1ODE1MjtlYlZOalo2eGNwNkFyOXdpVE52Q0RRKio7UkVTVCBjbGllbnQ7IDsg;Path=/;HttpOnly
    Expires: Thu, 01 Jan 1970 00:00:00 GMT
    Set-Cookie: ALM_USER=b3a903d0544b5aaa1f150e2b8f6ad420b4e8ad33dea552dacd02479c4f07fe41;Path=/
    Set-Cookie: XSRF-TOKEN=ad3d73000606ca7bf6aa5773344720f7bbe0201c154e51c8a770a0bb29e0db38;Path=/
    Content-Length: 0
    Server: Jetty(9.1.4.v20140401)';

function get_headers_from_curl_response($response)
{
    $headers = [];

    $header_text = substr($response, 0, strpos($response, "\r\n\r\n"));

    foreach (explode("\r\n", $header_text) as $i => $line)
        if ($i === 0)
            $headers['http_code'] = $line;
        else {
            list ($key, $value) = explode(': ', $line);

            $headers[$key] = $value;
        }

    return $headers;
}

$headers = get_headers_from_curl_response($response);
print_r($headers);

But I am getting only

Array
(
    [http_code] => HTTP/1.1 200 OK
    [Set-Cookie] => XSRF-TOKEN=4891ba55b486a006c130679763e369e15f309fbd6254c7e9c92bc8493f48eccd;Path=/
    [Expires] => Thu, 01 Jan 1970 00:00:00 GMT
    [Content-Length] => 0
    [Server] => Jetty(9.1.4.v20140401)
)
2
  • Is that one, long string? You could do this by stripping off all the things you don't need, and then use explode(), but it would be prudent to know if it's one long string, or multiple strings. Commented Oct 30, 2016 at 13:05
  • I just updated my approach can u please check that and see what i have done wrong Commented Oct 30, 2016 at 13:10

1 Answer 1

1

This should work perfectly

<?php

$string = 'HTTP/1.1 200 OK
Set-Cookie: LWSSO_COOKIE_KEY=UUo07jYJvrMtRg_GbKTV5Tf2hmI6N4XQRyVR7lLl9M6PphiPdmSWwLU7Pr1ZMRra10gv4HGLw9PlKDvyhPD0Z23u-Mv2S8zXf5Jgt67JGOV06J7d3I_ne7KmbbhwBIpvisedcBhBDLXfrOP-aFvJvdYekAtikN0GAU4Q2eTt1GWxPOJrRQUS2sSWXKC0A5NKUszUwkg11oBe-sXhsLmy5NiSDSErGNt_XBowWA-AgT2CFlB3J-xpUQ1xupJ8rs4H3A1u8zIGtlIWKtawLQLaKQYhQwr8DXnJUxpt0YNiyEg.;Path=/;HTTPOnly
Set-Cookie: QCSession=NzE1ODE1MjtlYlZOalo2eGNwNkFyOXdpVE52Q0RRKio7UkVTVCBjbGllbnQ7IDsg;Path=/;HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Set-Cookie: ALM_USER=b3a903d0544b5aaa1f150e2b8f6ad420b4e8ad33dea552dacd02479c4f07fe41;Path=/
Set-Cookie: XSRF-TOKEN=ad3d73000606ca7bf6aa5773344720f7bbe0201c154e51c8a770a0bb29e0db38;Path=/
Content-Length: 0
Server: Jetty(9.1.4.v20140401)';

$array_start = explode(';',$string);

foreach ($array_start as $key => $value) {
    $remove_from_string = ['HTTP/1.1 200 OK','Path=/','HTTPOnly','HttpOnly','Content-Length',': 0'];
    $replace_array = ['','','','','',''];
    $value = str_replace($remove_from_string,$replace_array,$value);
    $value = trim(preg_replace(('/Expires: [a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ [a-zA-Z]+/'), '', $value));
    $value = trim(preg_replace(('/Server: [a-zA-Z0-9.\(\)]+/'),'',$value));
    if (!empty($value)) {
        $new_array[trim(explode('=',$value)[0])] = explode('=',$value)[1];   
    }
}
$good = ['Set-Cookie: LWSSO_COOKIE_KEY','Set-Cookie: QCSession','Set-Cookie: ALM_USER'];
$result = array_intersect_key($new_array,array_flip($good));

echo '<pre>';
print_r($result);
echo '</pre>';

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Now you can get the ALM_USER as well, as well as the last entry, the Set-Cookie: XSRF-TOKEN if you add that as well to the $good-array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.