HTTP Headers Util - Header value parsing utility functions
This class is based on HTTP::Headers::Util from Perl's LWP <http://search.cpan.org/author/GAAS/libwww-perl-5.65/lib/HTTP/Headers/Util.pm> by Gisle Aas. The text here is copied from the documentation of the above, obviously slightly modified as this is PHP not Perl.
SYNOPSIS
1 $values = HTTP_Headers_Util::split_header_words($header_value);
DESCRIPTION
This class provides a few functions that helps parsing and construction of valid HTTP header values.
Located in Program_Root/HTTP_Headers_Util.php (line 45)
join_header_words
This will do the opposite of the conversion done by split_header_words(). It takes a list of anonymous arrays as arguments (or a list of key/value pairs) and produces a single header value. Attribute values are quoted if needed.
Example:
will both return the string:
1 join_header_words(array(array("text/plain" => null, "charset" => "iso-8859/1")));
2 join_header_words(array("text/plain" => null, "charset" => "iso-8859/1"));
text/plain; charset="iso-8859/1"
split_header_words
This function will parse the header values given as argument into a array containing key/value pairs. The function knows how to deal with ",", ";" and "=" as well as quoted values after "=". A list of space separated tokens are parsed as if they were separated by ";".
If the $header_values passed as argument contains multiple values, then they are treated as if they were a single value separated by comma ",".
This means that this function is useful for parsing header fields that follow this syntax (BNF as from the HTTP/1.1 specification, but we relax the requirement for tokens).
headers = #header header = (token | parameter) *( [";"] (token | parameter)) token = 1*Each header is represented by an anonymous array of key/value pairs. The value for a simple token (not part of a parameter) is null. Syntactically incorrect headers will not necessary be parsed as you would want.separators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" | <"> | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT quoted-string = ( <"> *(qdtext | quoted-pair ) <"> ) qdtext = > quoted-pair = "\" CHAR parameter = attribute "=" value attribute = token value = token | quoted-string
This is easier to describe with some examples:
will return
1 split_header_words('foo="bar"; port="80,81"; discard, bar=baz');
2 split_header_words('text/html; charset="iso-8859-1");
3 split_header_words('Basic realm="\"foo\\bar\""');
[foo=>'bar', port=>'80,81', discard=>null], [bar=>'baz']
['text/html'=>null, charset=>'iso-8859-1']
[Basic=>null, realm=>'"foo\bar"']
string or array
Documention generated on Wed, 16 Jul 2003 01:03:24 +0100 by phpDocumentor 1.2.0