<!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)</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> )</div> <p class="para rdfs-comment"> Unpacks from a binary string into an array according to the given <em><code class="parameter">format</code></em>. </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> <span class="term"><em><code class="parameter">format</code></em></span> <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> <dt> <span class="term"><em><code class="parameter">data</code></em></span> <dd> <p class="para"> The packed data. </p> </dd> </dt> </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>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-3789"> <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">?></span> </span> </code></div> </div> <div class="example-contents"><p> The resulting array will contain the entries "chars" with value <em>4</em> and "int" with <em>160</em>. </p></div> </div> </p> <p class="para"> <div class="example" id="example-3790"> <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">?></span> </span> </code></div> </div> <div class="example-contents"><p> The resulting array will contain the entries "chars1", "chars2" and "int". </p></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"> Be aware that if you do not name an element, an empty string is used. If you do not name more than one element, this means that some data is overwritten as the keys are the same such as in: </p> <p class="para"> <div class="example" id="example-3791"> <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 resulting array will contain the entries "1" with value <em>160</em> and "2" with <em>66</em>. 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>