note 104511 added to function.http-parse-headers
| From: | Ej52 at osu1 dot php dot net | Date: | Sun, 19 Jun 2011 21:00:46 +0000 |
| Subject: | note 104511 added to function.http-parse-headers | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180670@lists.php.net to get a copy of this message | ||
The previous submission was good, however when there is more than one
of the same header the function returns a really deep array for that header
Example:
[Set-Cookie] => Array
(
[0] => Array
(
[0] => Array
(
[0] => cookie1=test1;
[1] => cookie2=test2;
)
[1] => cookie3=test3;
)
[1] => cookie4=test4;
)
So i just modified the previous function so it would create a single array of
all the headers that are the same which will produce the following.
Example:
[Set-Cookie] => Array
(
[0] => cookie1=test1;
[1] => cookie2=test2;
[2] => cookie3=test3;
[3] => cookie4=test4;
)
And here is the modified version for those of you not wanting to install PECL :)
<?php
function http_parse_headers( $data ) {
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ',
$data));
foreach( $fields as $field ) {
if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
$match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e',
'strtoupper("\0")', strtolower(trim($match[1])));
if( isset($retVal[$match[1]]) ) {
if ( is_array( $retVal[$match[1]] ) ) {
$i = count($retVal[$match[1]]);
$retVal[$match[1]][$i] = $match[2];
}
else {
$retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
}
} else {
$retVal[$match[1]] = trim($match[2]);
}
}
}
return $retVal;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 87.236.194.70 (proxied: 197.168.0.25)
----
Manual Page -- http://www.php.net/manual/en/function.http-parse-headers.php
Edit -- https://master.php.net/note/edit/104511
Del: integrated -- https://master.php.net/note/delete/104511/integrated
Del: useless -- https://master.php.net/note/delete/104511/useless
Del: bad code -- https://master.php.net/note/delete/104511/bad+code
Del: spam -- https://master.php.net/note/delete/104511/spam
Del: non-english -- https://master.php.net/note/delete/104511/non-english
Del: in docs -- https://master.php.net/note/delete/104511/in+docs
Del: other reasons-- https://master.php.net/note/delete/104511
Reject -- https://master.php.net/note/reject/104511
Search -- https://master.php.net/manage/user-notes.php