Batman Batman - 1 month ago 6
PHP Question

Parsing string in PHP and create an array

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)
)

Answer

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>';

?>