<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.misc.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.unpack.php',
    1 => 'unpack',
    2 => 'Unpack data from binary string',
  ),
  'up' => 
  array (
    0 => 'ref.misc.php',
    1 => '杂项 函数',
  ),
  'prev' => 
  array (
    0 => 'function.uniqid.php',
    1 => 'uniqid',
  ),
  'next' => 
  array (
    0 => 'function.usleep.php',
    1 => 'usleep',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/misc/functions/unpack.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.unpack" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">unpack</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">unpack</span> &mdash; <span class="dc-title">Unpack data from binary string</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.unpack-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>unpack</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$format</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></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>
  <p class="para">
   Changes were made to bring this function into line with Perl:
   <ul class="simplelist">
    <li>
     The "a" code now retains trailing NULL bytes.
    </li>
    <li>
     The "A" code now strips all trailing ASCII whitespace (spaces, tabs,
     newlines, carriage returns, and NULL bytes).
    </li>
    <li>
     The "Z" code was added for NULL-padded strings, and removes trailing
     NULL bytes.
    </li>
   </ul>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.unpack-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">format</code></dt>
     <dd>
      <p class="para">
       See <span class="function"><a href="function.pack.php" class="function">pack()</a></span> for an explanation of the format codes.
      </p>
     </dd>
    
    
     <dt><code class="parameter">string</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">返回值</h3>
  <p class="para">
   Returns an associative array containing unpacked elements of binary
   string,  或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.unpack-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.2.0</td>
       <td>
        <span class="type"><a href="language.types.float.php" class="type float">float</a></span> and <span class="type"><a href="language.types.float.php" 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>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.unpack-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #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">&lt;?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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例会输出：</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Array
(
    [chars] =&gt; 4
    [int] =&gt; 160
)</pre>
</div>
    </div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-2">
    <p><strong>示例 #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">&lt;?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">?&gt;</span></span></code></div>
     </div>

     <div class="example-contents"><p>以上示例会输出：</p></div>
     <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Array
(
    [chars1] =&gt; 4
    [chars2] =&gt; 0
    [int] =&gt; 40960
)</pre>
</div>
     </div>
    </div>
   </p>
  </div>


 <div class="refsect1 notes" id="refsect1-function.unpack-notes">
  <h3 class="title">注释</h3>
  <div class="caution"><strong class="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">警告</strong>
   <p class="para">
    If you do not name an element, numeric indices starting from <code class="literal">1</code> are used.
    Be aware that if you have more than one unnamed element, some data is
    overwritten because the numbering restarts from <code class="literal">1</code> for each element.
   </p>
   <p class="para">
    <div class="example" id="example-3">
     <p><strong>示例 #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">&lt;?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">?&gt;</span></span></code></div>
     </div>

     <div class="example-contents"><p>以上示例会输出：</p></div>
     <div class="example-contents screen">
<div class="notescode"><pre class="notescode">array(2) {
  [1]=&gt;
  int(160)
  [2]=&gt;
  int(66)
}</pre>
</div>
     </div>
     <div class="example-contents"><p>
      Note that the
      first value from the <code class="literal">c</code> specifier is 
      overwritten by the first value from the <code class="literal">n</code>
      specifier.
     </p></div>
    </div>
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.unpack-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.pack.php" class="function" rel="rdfs-seeAlso">pack()</a> - 将数据打包成二进制字符串</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>