<?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.substr.php',
    1 => 'substr',
    2 => 'Retourne un segment de cha&icirc;ne',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'Fonctions sur les cha&icirc;nes de caract&egrave;res',
  ),
  'prev' => 
  array (
    0 => 'function.strtr.php',
    1 => 'strtr',
  ),
  'next' => 
  array (
    0 => 'function.substr-compare.php',
    1 => 'substr_compare',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/strings/functions/substr.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.substr" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">substr</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">substr</span> &mdash; <span class="dc-title">Retourne un segment de chaîne</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.substr-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>substr</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.integer.php" class="type int">int</a></span> <code class="parameter">$offset</code></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$length</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   Retourne le segment de <code class="parameter">string</code> défini par
   <code class="parameter">offset</code> et <code class="parameter">length</code>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.substr-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 <a href="language.types.string.php" class="link">chaîne de caractères</a> d&#039;entrée.
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       Si <code class="parameter">offset</code> est positif, la chaîne
       retournée commencera au caractère numéro <code class="parameter">offset</code>,
       dans la chaîne <code class="parameter">string</code>. Le premier caractère
       est numéroté zéro. En effet, dans la chaîne &#039;<code class="literal">abcdef</code>&#039;,
       le caractère à la position <code class="literal">0</code> est &#039;<code class="literal">a</code>&#039;,
       le caractère à la position <code class="literal">2</code> est &#039;<code class="literal">c</code>&#039;,
       et ainsi de suite.
      </p>
      <p class="para">
       Si <code class="parameter">offset</code> est négatif, la chaîne retournée
       commencera au caractère numéro <code class="parameter">offset</code> à compter
       de la fin de la chaîne <code class="parameter">string</code>.
      </p>
      <p class="para">
       Si <code class="parameter">string</code> est plus petit que
       <code class="parameter">offset</code> caractères de long, une chaîne vide sera retournée.
      </p>
       <p class="para">
       <div class="example" id="example-1">
        <p><strong>Exemple #1 Exemple de <code class="parameter">offset</code> négatif</strong></p>
        <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// retourne "f"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">2</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;    </span><span style="color: #FF8000">// retourne "ef"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// retourne "d"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
        </div>

       </div>
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       Si <code class="parameter">length</code> est fourni et est positif,
       la chaîne retournée contiendra au plus <code class="parameter">length</code>
       caractères, en commençant à partir du caractère
       <code class="parameter">offset</code> (en fonction de la taille de la chaîne
       <code class="parameter">string</code>).
      </p>
      <p class="para">
       Si <code class="parameter">length</code> est fourni et négatif, alors le même
       caractère sera omis de la fin de <code class="parameter">string</code>.
       Si <code class="parameter">offset</code> indique la position de cette troncature ou
       une position hors de la chaîne, une chaîne vide sera retournée.
      </p>
      <p class="para">
       Si le paramètre <code class="parameter">length</code> est fourni
       et vaut <code class="literal">0</code>, une chaîne vide sera retournée.
      </p>
      <p class="para">
       Si <code class="parameter">length</code> est omis ou <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>, la sous chaîne commençant
       à partir de <code class="parameter">offset</code> jusqu&#039;à la fin sera retournée.
      </p>
      <div class="example" id="example-2">
       <p><strong>Exemple #2 Utilisation d&#039;une valeur négative pour <code class="parameter">length</code></strong></p>
       <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// retourne "abcde"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// retourne "cde"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, -</span><span style="color: #0000BB">4</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// retourne ""; antérieur à PHP 8.0.0, false était retourné<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, -</span><span style="color: #0000BB">3</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// retourne "de"<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
       </div>

      </div>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.substr-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Retourne la partie extraite de la chaîne <code class="parameter">string</code>,
   ou une chaîne vide.
  </p>
 </div>


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

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">length</code> est désormais nullable.
       Lorsque <code class="parameter">length</code> est explicitement défini à <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>,
       la fonction retourne une sous-chaîne terminant à la fin de la chaîne,
       alors qu&#039;elle renvoyait auparavant une chaîne vide.
      </td>
     </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       Cette fonction retourne une chaîne vide où auparavant elle retournait <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.substr-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Exemple avec <span class="function"><strong>substr()</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 /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;     </span><span style="color: #FF8000">// bcdef<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"abcdef"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">null</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// bcdef; antérieur à PHP 8.0.0, une chaîne vide était retournée<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// bcd<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// abcd<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">8</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;  </span><span style="color: #FF8000">// abcdef<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// f<br /><br />// Accéder à un simple caractère dans une chaîne<br />// peut également être réalisé en utilisant des crochets<br /></span><span style="color: #0000BB">$string </span><span style="color: #007700">= </span><span style="color: #DD0000">'abcdef'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$string</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;                 </span><span style="color: #FF8000">// a<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$string</span><span style="color: #007700">[</span><span style="color: #0000BB">3</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;                 </span><span style="color: #FF8000">// d<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">$string</span><span style="color: #007700">[</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)-</span><span style="color: #0000BB">1</span><span style="color: #007700">], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// f<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-4">
    <p><strong>Exemple #4 Comportement du cast avec <span class="function"><strong>substr()</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 /></span><span style="color: #007700">class </span><span style="color: #0000BB">apple </span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__toString</span><span style="color: #007700">() {<br />        return </span><span style="color: #DD0000">"green"</span><span style="color: #007700">;<br />    }<br />}<br /><br />echo </span><span style="color: #DD0000">"1) "</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">"pear"</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"2) "</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">54321</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"3) "</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(new </span><span style="color: #0000BB">apple</span><span style="color: #007700">(), </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"4) "</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"5) "</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"6) "</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"7) "</span><span style="color: #007700">, </span><span style="color: #0000BB">var_export</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">1.2e3</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">), </span><span style="color: #0000BB">true</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</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">1) &#039;pe&#039;
2) &#039;54&#039;
3) &#039;gr&#039;
4) &#039;1&#039;
5) &#039;&#039;
6) &#039;&#039;
7) &#039;1200&#039;</pre>
</div>
    </div>
   </div>
  </p>

  <div class="example" id="example-5">
   <p><strong>Exemple #5 Intervalle de Caractères Invalide</strong></p>
   <div class="example-contents"><p>
    Si un intervalle de caractères invalide est demandé,
    <span class="function"><strong>substr()</strong></span> retourne une chaîne vide à partir de PHP 8.0.0;
    auparavant <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> était retourné à la place.
   </p></div>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus en PHP 8 :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">string(0) &quot;&quot;</pre>
</div>
   </div>
   <div class="example-contents"><p>Résultat de l&#039;exemple ci-dessus en PHP 7 :</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">bool(false)</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.substr-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.strrchr.php" class="function" rel="rdfs-seeAlso">strrchr()</a> - Trouve la derni&egrave;re occurrence d'un caract&egrave;re dans une cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.substr-replace.php" class="function" rel="rdfs-seeAlso">substr_replace()</a> - Remplace un segment dans une cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.preg-match.php" class="function" rel="rdfs-seeAlso">preg_match()</a> - Effectue une recherche de correspondance avec une expression rationnelle standard</span></li>
    <li><span class="function"><a href="function.trim.php" class="function" rel="rdfs-seeAlso">trim()</a> - Supprime les espaces (ou d'autres caract&egrave;res) en d&eacute;but et fin de cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.mb-substr.php" class="function" rel="rdfs-seeAlso">mb_substr()</a> - Lit une sous-cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.wordwrap.php" class="function" rel="rdfs-seeAlso">wordwrap()</a> - Effectue la c&eacute;sure d'une cha&icirc;ne</span></li>
    <li><a href="language.types.string.php#language.types.string.substr" class="link">Accès et modification d&#039;une chaîne, par caractère</a></li>
   </ul>
  </p>
 </div>


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