header parser docs
[mdref/mdref-http] / http / Header / Parser / parse.md
1 # int http\Header\Parser::parse(string $data, int $flags, array &$message = NULL)
2
3 Parse a string.
4
5 ## Params:
6
7 * string $data
8 The (part of the) header to parse.
9 * int $flags
10 Any combination of [parser flags](http/Header/Parser#Parser.flags:).
11 * array &$header = NULL
12 Successfully parsed headers.
13
14 ## Returns:
15
16 * int, http\Header\Parser::STATE_* constant.
17
18 ## Throws:
19
20 * http\Exception\InvalidArgumentException
21
22 ## Example:
23
24 <?php
25
26 $headers = [
27 "One: ","header\n",
28 "Two: header\n\tlines\n",
29 "Three",": header\n lines\n here\n",
30 "More: than one header\n",
31 "More: ", "than: ", "you: ", "expect\n",
32 "\n",
33 ];
34
35 $states = [-1=>"FAILURE",0=>"START","KEY","VALUE","VALUE_EX","HEADER_DONE","DONE"];
36 $parser = new http\Header\Parser;
37 do {
38 $state = $parser->parse($part = array_shift($headers),
39 $headers ? 0 : http\Header\Parser::CLEANUP,
40 $result);
41 printf("%2\$-32s | %1\$s\n", $states[$state], addcslashes($part, "\r\n\t\0"));
42 } while ($headers && $state !== http\Header\Parser::STATE_FAILURE);
43
44 var_dump($result);
45
46 ?>
47
48 Yields:
49
50 Test
51 One: | VALUE
52 header\n | VALUE_EX
53 Two: header\n\tlines\n | VALUE_EX
54 Three | KEY
55 : header\n lines\n here\n | VALUE_EX
56 More: than one header\n | VALUE_EX
57 More: | VALUE
58 than: | VALUE
59 you: | VALUE
60 expect\n | VALUE_EX
61 \n | DONE
62 array(4) {
63 ["One"]=>
64 string(6) "header"
65 ["Two"]=>
66 string(12) "header lines"
67 ["Three"]=>
68 string(17) "header lines here"
69 ["More"]=>
70 array(2) {
71 [0]=>
72 string(15) "than one header"
73 [1]=>
74 string(17) "than: you: expect"
75 }
76 }