<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Unpack data from binary string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.uniqid.html">uniqid</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.usleep.html">usleep</a></div> <div class="up"><a href="ref.misc.html">Misc. Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.unpack" class="refentry"> <div class="refnamediv"> <h1 class="refname">unpack</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">unpack</span> — <span class="dc-title">Unpack data from binary string</span></p> </div> <div class="refsect1 description" id="refsect1-function.unpack-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>unpack</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> Unpacks from a binary string into an array according to the given <code class="parameter">format</code>. </p> <p class="para"> The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name. </p> </div> <div class="refsect1 parameters" id="refsect1-function.unpack-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">format</code></dt> <dd> <p class="para"> See <span class="function"><a href="function.pack.html" class="function">pack()</a></span> for an explanation of the format codes. </p> </dd> <dt> <code class="parameter">data</code></dt> <dd> <p class="para"> The packed data. </p> </dd> <dt> <code class="parameter">offset</code></dt> <dd> <p class="para"> The offset to begin unpacking from. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.unpack-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an associative array containing unpacked elements of binary string. </p> </div> <div class="refsect1 changelog" id="refsect1-function.unpack-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>7.2.0</td> <td> <span class="type"><a href="language.types.float.html" class="type float">float</a></span> and <span class="type"><a href="language.types.float.html" class="type double">double</a></span> types supports both Big Endian and Little Endian. </td> </tr> <tr> <td>7.1.0</td> <td> The optional <code class="parameter">offset</code> has been added. </td> </tr> <tr> <td>5.5.0</td> <td> <p class="para"> Changes were made to bring this function into line with Perl: </p> <p class="para"> The "a" code now retains trailing NULL bytes. </p> <p class="para"> The "A" code now strips all trailing ASCII whitespace (spaces, tabs, newlines, carriage returns, and NULL bytes). </p> <p class="para"> The "Z" code was added for NULL-padded strings, and removes trailing NULL bytes. </p> </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.unpack-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4681"> <p><strong>Example #1 <span class="function"><strong>unpack()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$binarydata </span><span style="color: #007700">= </span><span style="color: #DD0000">"\x04\x00\xa0\x00"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= </span><span style="color: #0000BB">unpack</span><span style="color: #007700">(</span><span style="color: #DD0000">"cchars/nint"</span><span style="color: #007700">, </span><span style="color: #0000BB">$binarydata</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [chars] => 4 [int] => 160 ) </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4682"> <p><strong>Example #2 <span class="function"><strong>unpack()</strong></span> example with a repeater</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$binarydata </span><span style="color: #007700">= </span><span style="color: #DD0000">"\x04\x00\xa0\x00"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= </span><span style="color: #0000BB">unpack</span><span style="color: #007700">(</span><span style="color: #DD0000">"c2chars/nint"</span><span style="color: #007700">, </span><span style="color: #0000BB">$binarydata</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Array ( [chars1] => 4 [chars2] => 0 [int] => 40960 ) </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.unpack-notes"> <h3 class="title">Notes</h3> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> Note that PHP internally stores integral values as signed. If you unpack a large unsigned long and it is of the same size as PHP internally stored values the result will be a negative number even though unsigned unpacking was specified. </p> </div> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> If you do not name an element, numeric indices starting from <em>1</em> are used. Be aware that if you have more than one unnamed element, some data is overwritten because the numbering restarts from <em>1</em> for each element. </p> <p class="para"> <div class="example" id="example-4683"> <p><strong>Example #3 <span class="function"><strong>unpack()</strong></span> example with unnamed keys</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$binarydata </span><span style="color: #007700">= </span><span style="color: #DD0000">"\x32\x42\x00\xa0"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= </span><span style="color: #0000BB">unpack</span><span style="color: #007700">(</span><span style="color: #DD0000">"c2/n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$binarydata</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(2) { [1]=> int(160) [2]=> int(66) } </pre></div> </div> <div class="example-contents"><p> Note that the first value from the <em>c</em> specifier is overwritten by the first value from the <em>n</em> specifier. </p></div> </div> </p> </div> </div> <div class="refsect1 seealso" id="refsect1-function.unpack-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.pack.html" class="function" rel="rdfs-seeAlso">pack()</a> - Pack data into binary string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.uniqid.html">uniqid</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.usleep.html">usleep</a></div> <div class="up"><a href="ref.misc.html">Misc. Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>