<?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 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.strtok.php',
    1 => 'strtok',
    2 => 'Coupe une cha&icirc;ne en segments',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'Fonctions sur les cha&icirc;nes de caract&egrave;res',
  ),
  'prev' => 
  array (
    0 => 'function.strstr.php',
    1 => 'strstr',
  ),
  'next' => 
  array (
    0 => 'function.strtolower.php',
    1 => 'strtolower',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    '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">Coupe une chaîne en segments</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.strtok-description">
  <h3 class="title">Description</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">Signature alternative (non supportée avec les arguments nommés) :</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> coupe la chaîne <code class="parameter">string</code>
   en segments, chaque segment étant délimité par <code class="parameter">token</code>.
   Par exemple, si l&#039;on a une chaîne telle que
   <code class="literal">&quot;Voici un bon exemple&quot;</code>, il est possible d&#039;en extraire les
   différents mots en utilisant l&#039;espace comme <code class="parameter">token</code>.
  </p>
  <p class="para">
   Il est à noter que seul le premier appel à <span class="function"><strong>strtok()</strong></span> utilise
   l&#039;argument <code class="parameter">string</code>.
   Tous les appels ultérieurs à <span class="function"><strong>strtok()</strong></span> nécessitent uniquement
   le <code class="parameter">token</code> à utiliser, car elle sait où elle se situe
   dans la chaîne courante.
   Pour recommencer ou découper une nouvelle chaîne il est possible de simplement
   appeler <span class="function"><strong>strtok()</strong></span> avec le paramètre <code class="parameter">string</code>
   de nouveau pour l&#039;initialiser. Il est à noter qu&#039;il est possible de mettre
   plusieurs tokens dans le paramètre <code class="parameter">token</code>.
   La chaîne <code class="parameter">string</code> sera découpée dès que l&#039;un des
   caractères de l&#039;argument <code class="parameter">token</code> est trouvé.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Cette fonction se comporte de manière légèrement différente de ce qu&#039;on
    peut s&#039;attendre étant familier avec <span class="function"><a href="function.explode.php" class="function">explode()</a></span>.
    Premièrement, une séquence de deux ou plus <code class="parameter">token</code>
    caractères contigus dans la chaîne analysée est considérée comme un unique
    délimiteur.
    De plus, un <code class="parameter">token</code> situé au début ou à la fin de la
    chaîne est ignoré.
    Par exemple, si la chaîne <code class="literal">&quot;;aaa;;bbb;&quot;</code> est utilisée,
    les appels successifs à <span class="function"><strong>strtok()</strong></span> avec
    <code class="literal">&quot;;&quot;</code> en tant que <code class="parameter">token</code> retournera
    les chaînes &quot;aaa&quot; et &quot;bbb&quot;, et puis <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
    Par conséquent, la chaîne sera juste séparée en deux éléments, tandis que
    <code class="literal">explode(&quot;;&quot;, $string)</code> retournerait un tableau de 5 éléments.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.strtok-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string</code></dt>
     <dd>
      <p class="para">
       La chaîne à découper en plusieurs chaînes de tailles plus petites (tokens).
      </p>
     </dd>
    
    
     <dt><code class="parameter">token</code></dt>
     <dd>
      <p class="para">
       Le délimiteur utilisé pour découper <code class="parameter">string</code>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.strtok-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Une <span class="type"><a href="language.types.string.php" class="type string">string</a></span> coupée, ou <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> si plus aucun token n&#039;est disponible.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.strtok-changelog">
  <h3 class="title">Historique</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.3.0</td>
       <td>
        Émet désormais un <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> lorsque le <code class="parameter">token</code> n&#039;est pas fourni.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.strtok-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Exemple avec <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">// Utiliser aussi les nouvelles lignes et les tabulations <br />// comme séparateur de mots<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>Exemple #2 Comportement de <span class="function"><strong>strtok()</strong></span> sur une partie vide trouvée</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>L&#039;exemple ci-dessus va afficher :</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>Exemple #3 La différence entre <span class="function"><strong>strtok()</strong></span> et <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 /><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 /><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>L&#039;exemple ci-dessus va afficher :</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">Notes</h3>
  <div class="warning"><strong class="warning">Avertissement</strong><p class="simpara">
 Cette fonction peut retourner <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, mais elle peut aussi retourner une valeur équivalent à <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
 Veuillez lire la section sur les <a href="language.types.boolean.php" class="link">booléens</a> pour plus d&#039;informations.
 Utilisez l&#039;<a href="language.operators.comparison.php" class="link">opérateur ===</a>
 pour tester la valeur de retour exacte de cette fonction.
</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.strtok-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.explode.php" class="function" rel="rdfs-seeAlso">explode()</a> - Scinde une cha&icirc;ne de caract&egrave;res en segments</span></li>
   </ul>
  </p>
 </div>


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