<?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-count.php',
    1 => 'substr_count',
    2 => 'Compte le nombre d\'occurrences de segments dans une cha&icirc;ne',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'Fonctions sur les cha&icirc;nes de caract&egrave;res',
  ),
  'prev' => 
  array (
    0 => 'function.substr-compare.php',
    1 => 'substr_compare',
  ),
  'next' => 
  array (
    0 => 'function.substr-replace.php',
    1 => 'substr_replace',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/strings/functions/substr-count.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.substr-count" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">substr_count</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">substr_count</span> &mdash; <span class="dc-title">Compte le nombre d&#039;occurrences de segments dans une chaîne</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.substr-count-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>substr_count</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$haystack</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$needle</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<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>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<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><br>): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>substr_count()</strong></span> retourne le nombre d&#039;occurrences
   de <code class="parameter">needle</code> dans la chaîne
   <code class="parameter">haystack</code>. Il est à noter que <code class="parameter">needle</code>
   est sensible à la casse.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Cette fonction ne compte pas les chaînes de caractères qui se recouvrent.
    Voir l&#039;exemple ci-dessous !
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.substr-count-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">haystack</code></dt>
     <dd>
      <p class="para">
       La chaîne de caractères pour rechercher à l&#039;intérieur
      </p>
     </dd>
    
    
     <dt><code class="parameter">needle</code></dt>
     <dd>
      <p class="para">
       La chaîne de caractères que l&#039;on recherche
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       Le décalage où on commence à compter. Si la position est négative,
       le comptage commence depuis la fin de la chaîne.
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       La taille maximale après le décalage spécifié pour rechercher la
       chaîne. La fonction émet une erreur si le décalage plus la taille est
       plus grand que la taille de <code class="parameter">haystack</code>.
       Une taille négative fera que le comptage commencera à la fin de
       <code class="parameter">haystack</code>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.substr-count-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Cette fonction retourne un <a href="language.types.integer.php" class="link">entier</a>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.substr-count-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.0.0</td>
      <td>
       <code class="parameter">length</code> est désormais nullable.
      </td>
     </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        Ajout du support des nombres négatifs pour <code class="parameter">offset</code>
        et <code class="parameter">length</code>.
        <code class="parameter">length</code> peut aussi être <code class="literal">0</code> maintenant.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.substr-count-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>substr_count()</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 />$text </span><span style="color: #007700">= </span><span style="color: #DD0000">'Ceci est un test'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// 16<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">'est'</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">; </span><span style="color: #FF8000">// 2<br /><br />// la chaîne de caractères est réduite à 'st un test', alors elle affiche 1<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">'est'</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// le texte est réduit à 'st u', alors elle affiche 0<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">'is'</span><span style="color: #007700">, </span><span style="color: #0000BB">3</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">;<br /><br /></span><span style="color: #FF8000">// affiche seulement 1, parce qu'elle ne compte pas les chaînes de caractères<br />// qui se recouvrent<br /></span><span style="color: #0000BB">$text2 </span><span style="color: #007700">= </span><span style="color: #DD0000">'gcdgcdgcd'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text2</span><span style="color: #007700">, </span><span style="color: #DD0000">'gcdgcd'</span><span style="color: #007700">), </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// lève une exception parce que 5+10 &gt; 14<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">, </span><span style="color: #DD0000">'is'</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">10</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>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.substr-count-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.count-chars.php" class="function" rel="rdfs-seeAlso">count_chars()</a> - Retourne des statistiques sur les caract&egrave;res utilis&eacute;s dans une cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.strpos.php" class="function" rel="rdfs-seeAlso">strpos()</a> - Cherche la position de la premi&egrave;re occurrence dans une cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.substr.php" class="function" rel="rdfs-seeAlso">substr()</a> - Retourne un segment de cha&icirc;ne</span></li>
    <li><span class="function"><a href="function.strstr.php" class="function" rel="rdfs-seeAlso">strstr()</a> - Trouve la premi&egrave;re occurrence dans une cha&icirc;ne</span></li>
   </ul>
  </p>
 </div>

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