Skip to content

Instantly share code, notes, and snippets.

@ePirat
Created April 2, 2014 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ePirat/9937111 to your computer and use it in GitHub Desktop.
Save ePirat/9937111 to your computer and use it in GitHub Desktop.
<?php
function dirtyHeaderParser($headers, $strict = true){
$arr = array();
$s = strtok($headers, ':');
while ($s){
if ( ($s[0] === ' ') || ($s[0] === "\t") ){
if (count($arr) != 0){
$tail = strtok('');
$tail = "{$s}:{$tail}";
$v = strtok($tail, "\n");
if (is_array($arr[$key])){
end($arr[$key]);
$last = key($arr[$key]);
$arr[$key][$last] = "{$arr[$key][$last]}\n{$v}";
reset($arr[$key]);
} else {
$arr[$key] = "{$arr[$key]}\n{$v}";
}
}
} else {
$v = strtok("\n");
if ($v){
$key = strtolower($s);
if (((strpos($key, "\n") !== false) || (strpos($key, "\t") !== false) || (strpos($key, " ") !== false)) && $strict) {
return false;
}
if (array_key_exists($key, $arr)){
if (!is_array($arr[$key])){
$arr[$key] = array($arr[$key]);
}
$arr[$key][] = trim($v);
} else {
$arr[$key] = trim($v);
}
} else {
break;
}
}
$s = strtok(':');
}
return (count($arr) == 0) ? false : $arr;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment