<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pcre.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'function.preg-replace-callback.php',
    1 => 'preg_replace_callback',
    2 => 'Rechercher et remplacer par expression rationnelle standard en utilisant une fonction de callback',
  ),
  'up' => 
  array (
    0 => 'ref.pcre.php',
    1 => 'Fonctions PCRE',
  ),
  'prev' => 
  array (
    0 => 'function.preg-replace.php',
    1 => 'preg_replace',
  ),
  'next' => 
  array (
    0 => 'function.preg-replace-callback-array.php',
    1 => 'preg_replace_callback_array',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/pcre/functions/preg-replace-callback.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.preg-replace-callback" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">preg_replace_callback</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.5, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">preg_replace_callback</span> &mdash; <span class="dc-title">Rechercher et remplacer par expression rationnelle standard en utilisant une fonction de callback</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.preg-replace-callback-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>preg_replace_callback</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><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.array.php" class="type array">array</a></span></span> <code class="parameter">$pattern</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> <code class="parameter">$callback</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><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.array.php" class="type array">array</a></span></span> <code class="parameter">$subject</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">$limit</code><span class="initializer"> = -1</span></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 reference">&$count</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></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">$flags</code><span class="initializer"> = 0</span></span><br>): <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.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span></div>

  <p class="para rdfs-comment">
   Le comportement de <span class="function"><strong>preg_replace_callback()</strong></span> est presque
   identique à celui de <span class="function"><a href="function.preg-replace.php" class="function">preg_replace()</a></span>, hormis le fait
   qu&#039;à la place du paramètre <code class="parameter">replacement</code>, il faut
   spécifier une fonction de rappel <code class="parameter">callback</code>
   qui sera appelée, avec les éléments trouvés en arguments.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.preg-replace-callback-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">pattern</code></dt>
     <dd>
      <p class="para">
       Le masque à chercher. Il peut être une <a href="language.types.string.php" class="link">chaîne de caractères</a> ou un tableau contenant
       des chaînes.
      </p>
     </dd>
    
    
     <dt><code class="parameter">callback</code></dt>
     <dd>
      <p class="para">
       La fonction de rappel qui recevra le tableau d&#039;éléments
       trouvés dans la chaîne <code class="parameter">subject</code>.
       La fonction de rappel doit retourner la chaîne de
       remplacement. Voici la signature de la fonction de rappel :
      </p>
      <p class="para">
       <div class="methodsynopsis dc-description">
        <span class="methodname"><span class="replaceable">handler</span></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$matches</code></span>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

      </p>
      <p class="para">
       On aura souvent besoin de la fonction <code class="parameter">callback</code>
       avec <span class="function"><strong>preg_replace_callback()</strong></span> à un seul endroit.
       Dans ce cas, il est possible de simplement utiliser une
       <a href="functions.anonymous.php" class="link">fonction anonyme</a>
       pour déclarer une fonction de rappel pour la fonction
       <span class="function"><strong>preg_replace_callback()</strong></span>.
       En faisant cela, l&#039;on concentre toutes les routines liées
       au remplacement à un seul endroit, et on ne pollue pas
       l&#039;espace de noms de fonctions avec des fonctions à usage unique.
      </p>
      <p class="para">
       <div class="example" id="example-1">
        <p><strong>Exemple #1 <span class="function"><strong>preg_replace_callback()</strong></span> et
        fonction anonyme</strong></p>
        <div class="example-contents">
<div class="annotation-non-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// Un filtre de ligne de commande Unix pour convertir la première lettre<br />// des paragraphes (commençant par "&lt;p&gt;") en minuscule<br /><br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://stdin"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">) or die(</span><span style="color: #DD0000">"Impossible de lire la ligne de commande"</span><span style="color: #007700">);<br />while (!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">)) {<br />    </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br />        </span><span style="color: #DD0000">'|&lt;p&gt;\s*\w|'</span><span style="color: #007700">,<br />        function (</span><span style="color: #0000BB">$matches</span><span style="color: #007700">) {<br />            return </span><span style="color: #0000BB">strtolower</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />        },<br />        </span><span style="color: #0000BB">$line<br />    </span><span style="color: #007700">);<br />    echo </span><span style="color: #0000BB">$line</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
        </div>

       </div>
      </p>
     </dd>
    
    
     <dt><code class="parameter">subject</code></dt>
     <dd>
      <p class="para">
       La chaîne ou le tableau de chaînes à chercher et à remplacer.
      </p>
     </dd>
    
    
     <dt><code class="parameter">limit</code></dt>
     <dd>
      <p class="para">
       Le nombre maximal de remplacement pour chaque masque
       dans chaque chaîne <code class="parameter">subject</code>.
       Vaut par défaut <code class="literal">-1</code> (aucune limite).
      </p>
     </dd>
    
    
     <dt><code class="parameter">count</code></dt>
     <dd>
      <p class="para">
       Si fournie, cette variable sera remplie avec le nombre de remplacements effectués.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">flags</code> peut être une combinaison des drapeaux
       <strong><code><a href="pcre.constants.php#constant.preg-offset-capture">PREG_OFFSET_CAPTURE</a></code></strong> et
       <strong><code><a href="pcre.constants.php#constant.preg-unmatched-as-null">PREG_UNMATCHED_AS_NULL</a></code></strong>, qui influence le format
       du tableau de correspondances.
       Voir la description de <span class="function"><a href="function.preg-match.php" class="function">preg_match()</a></span> pour plus de détail.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.preg-replace-callback-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   <span class="function"><strong>preg_replace_callback()</strong></span> retourne un tableau si le paramètre
   <code class="parameter">subject</code> est un <a href="language.types.array.php" class="link">tableau</a>, ou, sinon, une <a href="language.types.string.php" class="link">chaîne de caractères</a>.
   Si une erreur survient, la valeur retournée sera <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
  </p>
  <p class="para">
   Si des correspondances sont trouvées, le nouveau sujet sera retourné, sinon
   <code class="parameter">subject</code> sera retourné inchangé.
  </p>
 </div>

 
 <div class="refsect1 errors" id="refsect1-function.preg-replace-callback-errors">
  <h3 class="title">Erreurs / Exceptions</h3>
  <p class="para">
Si le masque regex passé ne compile pas à une regex valide, une <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> est émise.
</p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.preg-replace-callback-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>7.4.0</td>
       <td>
        Le paramètre <code class="parameter">flags</code> a été ajouté.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.preg-replace-callback-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Exemple #2 Exemple avec <span class="function"><strong>preg_replace_callback()</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: #FF8000">// Ce texte était vrai en 2002<br />// nous voulons le mettre a jour pour 2003<br /></span><span style="color: #0000BB">$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"Le premier avril est le 04/01/2002\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$text</span><span style="color: #007700">.= </span><span style="color: #DD0000">"Le dernier noël était le 12/24/2001\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Fonction de callback<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">next_year</span><span style="color: #007700">(</span><span style="color: #0000BB">$matches</span><span style="color: #007700">)<br />{<br />  </span><span style="color: #FF8000">// comme d'habitude : $matches[0] représente la valeur totale<br />  // $matches[1] représente la première parenthèse capturante<br />  </span><span style="color: #007700">return </span><span style="color: #0000BB">$matches</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">].(</span><span style="color: #0000BB">$matches</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">);<br />}<br />echo </span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br />            </span><span style="color: #DD0000">"|(\d{2}/\d{2}/)(\d{4})|"</span><span style="color: #007700">,<br />            </span><span style="color: #DD0000">"next_year"</span><span style="color: #007700">,<br />            </span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /><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">Le premier avril est le 04/01/2003
Le dernier noël était le 12/24/2002</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-3">
    <p><strong>Exemple #3 Exemple avec <span class="function"><strong>preg_replace_callback()</strong></span> en utilisant une structure récursive
     pour gérer du BB code</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$input </span><span style="color: #007700">= </span><span style="color: #DD0000">"plain [indent] deep [indent] deeper [/indent] deep [/indent] plain"</span><span style="color: #007700">;<br /><br />function </span><span style="color: #0000BB">parseTagsRecursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">)<br />{<br /><br />    </span><span style="color: #0000BB">$regex </span><span style="color: #007700">= </span><span style="color: #DD0000">'#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#'</span><span style="color: #007700">;<br /><br />    if (</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">)) {<br />        </span><span style="color: #0000BB">$input </span><span style="color: #007700">= </span><span style="color: #DD0000">'&lt;div style="margin-left: 10px"&gt;'</span><span style="color: #007700">.</span><span style="color: #0000BB">$input</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">].</span><span style="color: #DD0000">'&lt;/div&gt;'</span><span style="color: #007700">;<br />    }<br /><br />    return </span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$regex</span><span style="color: #007700">, </span><span style="color: #DD0000">'parseTagsRecursive'</span><span style="color: #007700">, </span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$output </span><span style="color: #007700">= </span><span style="color: #0000BB">parseTagsRecursive</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$output</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.preg-replace-callback-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><a href="pcre.pattern.php" class="link">Masques PCRE</a></li>
    <li><span class="function"><a href="function.preg-replace-callback-array.php" class="function" rel="rdfs-seeAlso">preg_replace_callback_array()</a> - Effectue une recherche de correspondance avec une expression r&eacute;guli&egrave;re et remplace gr&acirc;ce &agrave; une fonction de rappel</span></li>
    <li><span class="function"><a href="function.preg-quote.php" class="function" rel="rdfs-seeAlso">preg_quote()</a> - Protection des caract&egrave;res sp&eacute;ciaux des expressions r&eacute;guli&egrave;res</span></li>
    <li><span class="function"><a href="function.preg-replace.php" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Rechercher et remplacer par expression rationnelle standard</span></li>
    <li><span class="function"><a href="function.preg-last-error.php" class="function" rel="rdfs-seeAlso">preg_last_error()</a> - Retourne le code erreur de la derni&egrave;re expression PCRE ex&eacute;cut&eacute;e</span></li>
    <li><a href="functions.anonymous.php" class="link">Les fonctions anonymes</a></li>
   </ul>
  </p>
 </div>


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