parsing - How to best parse 1 HTTP header VALUE in PHP? -
i have important, detailed http header in response want parse value of details:
digest realm="my very, cool site", nonce="a8w/7adzbaa=ffc2afd053c8802dd64be69b985f38c85ec29607", algorithm=md5, domain="/", qop="auth"
it's comma-separated, it's quoted, values can contain ,
. there's "digest "
that's not part of key="value"
-pairs. (i can assume first part ("digest") never contain spaces, shouldn't hard.
i'm thinking sure way byte-by-byte parse , "
, ,
, that's lot of work.
it might i'm missing useful php spl function. tried http_parse_headers()
, that's unstandard , don't have that.
you can use regular expression:
function parseheader($theheadervalue /* without digest: part */) { if (preg_match_all("'(?<variable>[a-z0-9\_]+)\=(?<delimiter>[\"\']?)(?<value>.*?)(?<!\\\)\k<delimiter>(?:\s*(?:,|$))'isux", $theheadervalue, $matches) , !empty($matches['variable'])) { $result = array(); foreach ($matches['variable'] $index=>$key) $result[$key] = $matches['value'][$index]; return $result; } else return array(); }
Comments
Post a Comment