您好任何人都可以帮我解析PHP中的以下字符串并将其作为数组
该值由 ;
字符串值
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)
现在我希望数组具有以下值
[Set-Cookie: LWSSO_COOKIE_KEY] => UUo07jYJvrMtRg_GbKTV5Tf2hmI6N4XQRyVR7lLl9M6PphiPdmSWwLU7Pr1ZMRra10gv4HGLw9PlKDvyhPD0Z23u-Mv2S8zXf5Jgt67JGOV06J7d3I_ne7KmbbhwBIpvisedcBhBDLXfrOP-aFvJvdYekAtikN0GAU4Q2eTt1GWxPOJrRQUS2sSWXKC0A5NKUszUwkg11oBe-sXhsLmy5NiSDSErGNt_XBowWA-AgT2CFlB3J-xpUQ1xupJ8rs4H3A1u8zIGtlIWKtawLQLaKQYhQwr8DXnJUxpt0YNiyEg.
[Set-Cookie: QCSession] => NzE1ODE1MjtlYlZOalo2eGNwNkFyOXdpVE52Q0RRKio7UkVTVCBjbGllbnQ7IDsg
我试过下面的代码
$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);
但我只是
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)
)
答案 0 :(得分:1)
这应该可以完美地运作
<?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>';
?>