<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.strtok.php',
    1 => 'strtok',
    2 => '标记分割字符串',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => '字符串 函数',
  ),
  'prev' => 
  array (
    0 => 'function.strstr.php',
    1 => 'strstr',
  ),
  'next' => 
  array (
    0 => 'function.strtolower.php',
    1 => 'strtolower',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/strings/functions/strtok.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.strtok" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">strtok</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">strtok</span> &mdash; <span class="dc-title">标记分割字符串</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.strtok-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>strtok</strong></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.string.php" class="type string">string</a></span> <code class="parameter">$token</code></span>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="simpara">可选 token （不支持命名参数）：</p>
  <div class="methodsynopsis dc-description"><span class="methodname"><strong>strtok</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$token</code></span>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>strtok()</strong></span> 将字符串 <code class="parameter">string</code> 分割为若干子字符串，每个子字符串以
   <code class="parameter">token</code> 中的字符分割。这也就意味着，如果有个字符串是 &quot;This is an example string&quot;，
   你可以使用空格字符作为 <code class="parameter">token</code>，将这句话分割成独立的单词。
  </p>
  <p class="para">
   注意，仅第一次调用 strtok 函数时才使用 <code class="parameter">string</code> 参数。
   随后每次调用 strtok 都将只使用 <code class="parameter">token</code> 参数，因为它会记住它在当前字符串中的位置。
   如果要重新开始分割一个新的字符串，你需要再次使用 <code class="parameter">string</code> 参数来调用 strtok 函数来对其进行初始化。
   注意，可以在 <code class="parameter">token</code> 参数中使用多个字符。字符串将被 <code class="parameter">token</code> 参数中任何一个字符分割。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    这个函数的行为可能与熟悉 <span class="function"><a href="function.explode.php" class="function">explode()</a></span> 的人期望略有不同。
    首先，在解析的字符串中，两个或多个连续的 <code class="parameter">token</code> 的
    字符被认为是单一的分隔符。
    此外，位于字符串开始或结束处的 <code class="parameter">token</code> 将被忽略。
    例如，如果使用一个字符串 <code class="literal">&quot;;aaa;;bbb;&quot;</code> ，连续调用 <span class="function"><strong>strtok()</strong></span>
    并将 <code class="literal">&quot;;&quot;</code> 作为一个 <code class="parameter">token</code>
    将返回字符串 &quot;aaa&quot; 和 &quot;bbb&quot;，然后返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 。
    因此，字符串将仅被拆分为两个元素，而 <code class="literal">explode(&quot;;&quot;, $string)</code> 将返回一个包含 5 个元素的数组。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strtok-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string</code></dt>
     <dd>
      <p class="para">
       被分成若干子字符串的原始<span class="type">字符串</span>。
      </p>
     </dd>
    
    
     <dt><code class="parameter">token</code></dt>
     <dd>
      <p class="para">
       分割 <code class="parameter">string</code> 时使用的分界字符。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.strtok-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   标记后的<span class="type">字符串</span>，如果没有更多标记可用，则返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.strtok-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>8.3.0</td>
       <td>
        现在，当未提供 <code class="parameter">token</code> 时，会抛出 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.strtok-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>strtok()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">"This is\tan example\nstring"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">/* 使用制表符和换行符作为分界符 */<br /></span><span style="color: #0000BB">$tok </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #DD0000">" \n\t"</span><span style="color: #007700">);<br /><br />while (</span><span style="color: #0000BB">$tok </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"Word=</span><span style="color: #007700">{</span><span style="color: #0000BB">$tok</span><span style="color: #007700">}</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$tok </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">" \n\t"</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>示例 #2 当 <span class="function"><strong>strtok()</strong></span> 找不到标记时的反应</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$first_token  </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">'/something'</span><span style="color: #007700">, </span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$second_token </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">'/'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$first_token</span><span style="color: #007700">, </span><span style="color: #0000BB">$second_token</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="annotation-interactive examplescode"><pre class="examplescode">string(9) &quot;something&quot;
    bool(false)</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>示例 #3 <span class="function"><strong>strtok()</strong></span> 和 <span class="function"><a href="function.explode.php" class="function">explode()</a></span> 的不同点</strong></p>
    <div class="example-contents">
     <div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">";aaa;;bbb;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$parts </span><span style="color: #007700">= [];<br /></span><span style="color: #0000BB">$tok </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #DD0000">";"</span><span style="color: #007700">);<br />while (</span><span style="color: #0000BB">$tok </span><span style="color: #007700">!== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$parts</span><span style="color: #007700">[] = </span><span style="color: #0000BB">$tok</span><span style="color: #007700">;<br />    </span><span style="color: #0000BB">$tok </span><span style="color: #007700">= </span><span style="color: #0000BB">strtok</span><span style="color: #007700">(</span><span style="color: #DD0000">";"</span><span style="color: #007700">);<br />}<br />echo </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$parts</span><span style="color: #007700">),</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$parts </span><span style="color: #007700">= </span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">";"</span><span style="color: #007700">, </span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$parts</span><span style="color: #007700">),</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例会输出：</p></div>
    <div class="example-contents screen">
     <div class="annotation-interactive examplescode"><pre class="examplescode">[&quot;aaa&quot;,&quot;bbb&quot;]
[&quot;&quot;,&quot;aaa&quot;,&quot;&quot;,&quot;bbb&quot;,&quot;&quot;]</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.strtok-notes">
  <h3 class="title">注释</h3>
  
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">此函数可能返回布尔值
<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>，但也可能返回等同于 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 的非布尔值。请阅读 <a href="language.types.boolean.php" class="link">布尔类型</a>章节以获取更多信息。应使用
<a href="language.operators.comparison.php" class="link">===
运算符</a>来测试此函数的返回值。</p></div>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.strtok-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.explode.php" class="function" rel="rdfs-seeAlso">explode()</a> - 使用一个字符串分割另一个字符串</span></li>
   </ul>
  </p>
 </div>


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