reference/misc/functions/unpack.xml
5fabd07880ab15b0ad2cf7eb055c7c2b36d7120f
...
...
@@ -9,9 +9,10 @@
9
9
<refsect1 role="description">
10
10
&reftitle.description;
11
11
<methodsynopsis>
12
-
<type>array</type><methodname>unpack</methodname>
12
+
<type class="union"><type>array</type><type>false</type></type><methodname>unpack</methodname>
13
13
<methodparam><type>string</type><parameter>format</parameter></methodparam>
14
-
<methodparam><type>string</type><parameter>data</parameter></methodparam>
14
+
<methodparam><type>string</type><parameter>string</parameter></methodparam>
15
+
<methodparam choice="opt"><type>int</type><parameter>offset</parameter><initializer>0</initializer></methodparam>
15
16
</methodsynopsis>
16
17
<para>
17
18
Unpacks from a binary string into an array according to the given
...
...
@@ -24,6 +25,22 @@
24
25
then each of the array keys will have a sequence number behind
25
26
the given name.
26
27
</para>
28
+
<para>
29
+
Changes were made to bring this function into line with Perl:
30
+
<simplelist>
31
+
<member>
32
+
The "a" code now retains trailing NULL bytes.
33
+
</member>
34
+
<member>
35
+
The "A" code now strips all trailing ASCII whitespace (spaces, tabs,
36
+
newlines, carriage returns, and NULL bytes).
37
+
</member>
38
+
<member>
39
+
The "Z" code was added for NULL-padded strings, and removes trailing
40
+
NULL bytes.
41
+
</member>
42
+
</simplelist>
43
+
</para>
27
44
</refsect1>
28
45

29
46
<refsect1 role="parameters">
...
...
@@ -39,13 +56,21 @@
39
56
</listitem>
40
57
</varlistentry>
41
58
<varlistentry>
42
-
<term><parameter>data</parameter></term>
59
+
<term><parameter>string</parameter></term>
43
60
<listitem>
44
61
<para>
45
62
The packed data.
46
63
</para>
47
64
</listitem>
48
65
</varlistentry>
66
+
<varlistentry>
67
+
<term><parameter>offset</parameter></term>
68
+
<listitem>
69
+
<para>
70
+
The offset to begin unpacking from.
71
+
</para>
72
+
</listitem>
73
+
</varlistentry>
49
74
</variablelist>
50
75
</para>
51
76
</refsect1>
...
...
@@ -54,7 +79,7 @@
54
79
&reftitle.returnvalues;
55
80
<para>
56
81
Returns an associative array containing unpacked elements of binary
57
-
string.
82
+
string, &return.falseforfailure;.
58
83
</para>
59
84
</refsect1>
60
85

...
...
@@ -71,22 +96,15 @@
71
96
</thead>
72
97
<tbody>
73
98
<row>
74
-
<entry>5.5.0</entry>
99
+
<entry>7.2.0</entry>
75
100
<entry>
76
-
<para>
77
-
Changes were made to bring this function into line with Perl:
78
-
</para>
79
-
<para>
80
-
The "a" code now retains trailing NULL bytes.
81
-
</para>
82
-
<para>
83
-
The "A" code now strips all trailing ASCII whitespace (spaces, tabs,
84
-
newlines, carriage returns, and NULL bytes).
85
-
</para>
86
-
<para>
87
-
The "Z" code was added for NULL-padded strings, and removes trailing
88
-
NULL bytes.
89
-
</para>
101
+
<type>float</type> and <type>double</type> types supports both Big Endian and Little Endian.
102
+
</entry>
103
+
</row>
104
+
<row>
105
+
<entry>7.1.0</entry>
106
+
<entry>
107
+
The optional <parameter>offset</parameter> has been added.
90
108
</entry>
91
109
</row>
92
110
</tbody>
...
...
@@ -105,13 +123,20 @@
105
123
<?php
106
124
$binarydata = "\x04\x00\xa0\x00";
107
125
$array = unpack("cchars/nint", $binarydata);
126
+
print_r($array);
108
127
?>
109
128
]]>
110
129
</programlisting>
111
-
<para>
112
-
The resulting array will contain the entries "chars" with value
113
-
<literal>4</literal> and "int" with <literal>160</literal>.
114
-
</para>
130
+
&example.outputs;
131
+
<screen>
132
+
<![CDATA[
133
+
Array
134
+
(
135
+
[chars] => 4
136
+
[int] => 160
137
+
)
138
+
]]>
139
+
</screen>
115
140
</example>
116
141
</para>
117
142

...
...
@@ -123,13 +148,21 @@ $array = unpack("cchars/nint", $binarydata);
123
148
<?php
124
149
$binarydata = "\x04\x00\xa0\x00";
125
150
$array = unpack("c2chars/nint", $binarydata);
151
+
print_r($array);
126
152
?>
127
153
]]>
128
154
</programlisting>
129
-
<para>
130
-
The resulting array will contain the entries "chars1",
131
-
"chars2" and "int".
132
-
</para>
155
+
&example.outputs;
156
+
<screen>
157
+
<![CDATA[
158
+
Array
159
+
(
160
+
[chars1] => 4
161
+
[chars2] => 0
162
+
[int] => 40960
163
+
)
164
+
]]>
165
+
</screen>
133
166
</example>
134
167
</para>
135
168
</refsect1>
...
...
@@ -146,9 +179,9 @@ $array = unpack("c2chars/nint", $binarydata);
146
179
</caution>
147
180
<caution>
148
181
<para>
149
-
Be aware that if you do not name an element, an empty string is used.
150
-
If you do not name more than one element, this means
151
-
that some data is overwritten as the keys are the same such as in:
182
+
If you do not name an element, numeric indices starting from <literal>1</literal> are used.
183
+
Be aware that if you have more than one unnamed element, some data is
184
+
overwritten because the numbering restarts from <literal>1</literal> for each element.
152
185
</para>
153
186
<para>
154
187
<example>
...
...
@@ -162,9 +195,19 @@ var_dump($array);
162
195
?>
163
196
]]>
164
197
</programlisting>
198
+
&example.outputs;
199
+
<screen>
200
+
<![CDATA[
201
+
array(2) {
202
+
[1]=>
203
+
int(160)
204
+
[2]=>
205
+
int(66)
206
+
}
207
+
]]>
208
+
</screen>
165
209
<para>
166
-
The resulting array will contain the entries "1" with value
167
-
<literal>160</literal> and "2" with <literal>66</literal>. The
210
+
Note that the
168
211
first value from the <literal>c</literal> specifier is
169
212
overwritten by the first value from the <literal>n</literal>
170
213
specifier.
...
...
@@ -184,7 +227,6 @@ var_dump($array);
184
227
</refsect1>
185
228

186
229
</refentry>
187
-

188
230
<!-- Keep this comment at the end of the file
189
231
Local variables:
190
232
mode: sgml
191
233