downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/fr/function.preg-replace-callback.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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',
  ),
 
'up' =>
  array (
   
0 => 'ref.pcre.php',
   
1 => 'Fonctions PCRE',
  ),
 
'prev' =>
  array (
   
0 => 'function.preg-quote.php',
   
1 => 'preg_quote',
  ),
 
'next' =>
  array (
   
0 => 'function.preg-replace.php',
   
1 => 'preg_replace',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<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)</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>

 <a name="function.preg-replace-callback.description"></a><div class="refsect1 description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><b>preg_replace_callback</b></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$pattern</tt></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.callback" class="type callback">callback</a></span> <tt class="parameter">$callback</tt></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$subject</tt></span>
   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$limit</tt><span class="initializer">= -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter reference">&amp;$count</tt></span>
  ]] )</div>

  <p class="para rdfs-comment">
   Le comportement de <b>preg_replace_callback()</b> est presque
   identique à celui de <a href="function.preg-replace.php" class="function">preg_replace()</a>, hormis le fait
   qu&#039;à la place du paramètre <i><tt class="parameter">replacement</tt></i>
, il faut
   spécifier une fonction de rappel <i><tt class="parameter">callback</tt></i>

   qui sera appelée, avec les éléments trouvés en arguments.
  </p>
 </div>


 <a name="function.preg-replace-callback.parameters"></a><div class="refsect1 parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   </p><dl>

    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">pattern</tt></i>
</span>

     </dt><dd class="listitem">

      <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 class="varlistentry">

     <span class="term"><i><tt class="parameter">callback</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       La fonction de rappel qui recevra le tableau d&#039;éléments
       trouvés dans la chaîne <i><tt class="parameter">subject</tt></i>
.
       La fonction de rappel doit retourner la chaîne de
       remplacement.
      </p>
      <p class="para">
       Vous aurez souvent besoin de la fonction <i><tt class="parameter">callback</tt></i>

       avec <b>preg_replace_callback()</b> à un seul endroit.
       Dans ce cas, vous pouvez simplement utiliser la fonction
       <a href="function.create-function.php" class="function">create_function()</a> pour déclarer une fonction anonyme
       comme fonction de rappel pour <b>preg_replace_callback()</b>.
       En faisant cela, vous concentrez toutes les routines liées à
       votre remplacement à un seul endroit, et nous ne polluez par
       votre espace de noms de fonctions avec des fonctions à usage unique.
      </p>
      <p class="para">
       </p><div class="example">
        <p><b>Exemple #1 <b>preg_replace_callback()</b> et
        <a href="function.create-function.php" class="function">create_function()</a></b></p>
        <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Un&nbsp;filtre&nbsp;de&nbsp;ligne&nbsp;de&nbsp;commande&nbsp;Unix&nbsp;pour&nbsp;convertir&nbsp;les&nbsp;lettres<br />//&nbsp;en&nbsp;majuscule&nbsp;de&nbsp;début&nbsp;des&nbsp;paragraphe&nbsp;en&nbsp;minuscules<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"php://stdin"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">)&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Impossible&nbsp;de&nbsp;lire&nbsp;la&nbsp;ligne&nbsp;de&nbsp;commande"</span><span style="color: #007700">);<br />while&nbsp;(!</span><span style="color: #0000BB">feof</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'|&lt;p&gt;\s*\w|'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">create_function</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Les&nbsp;guillemets&nbsp;simples&nbsp;sont&nbsp;très&nbsp;importants&nbsp;ici<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;ou&nbsp;bien&nbsp;il&nbsp;faut&nbsp;protéger&nbsp;les&nbsp;caractères&nbsp;$&nbsp;avec&nbsp;\$<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'$matches'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'return&nbsp;strtolower($matches[0]);'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$line<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</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>
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">subject</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       La chaîne ou le tableau de chaînes à chercher et à remplacer.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">limit</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Le nombre maximal de remplacement pour chaque masque
       dans chaque chaîne <i><tt class="parameter">subject</tt></i>
.
       Vaut par défaut <i>-1</i> (aucune limite).
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">count</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Si fournie, cette variable sera remplie avec le nombre de remplacements effectués.
      </p>
     </dd>

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


 <a name="function.preg-replace-callback.returnvalues"></a><div class="refsect1 returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   <b>preg_replace_callback()</b> retourne un tableau si le paramètre
   <i><tt class="parameter">subject</tt></i>
 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>.
  </p>
  <p class="para">
   Si des correspondances sont trouvées, le nouveau sujet sera
   retourné, sinon <i><tt class="parameter">subject</tt></i>
 sera retourné,
   inchangé.
  </p>
 </div>


 <a name="function.preg-replace-callback.changelog"></a><div class="refsect1 changelog">
  <h3 class="title">Historique</h3>
  <p class="para">
   </p><table class="doctable informaltable">
   
     <thead valign="middle">
      <tr valign="middle">
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody valign="middle" class="tbody">
      <tr valign="middle">
       <td align="left">5.1.0</td>
       <td align="left">
        Le paramètre <i><tt class="parameter">count</tt></i>
 a été ajouté.
       </td>
      </tr>

     </tbody>
   
   </table>
<p>
  </p>
 </div>


 <a name="function.preg-replace-callback.examples"></a><div class="refsect1 examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   </p><div class="example">
    <p><b>Exemple #2 Exemple avec <b>preg_replace_callback()</b></b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Ce&nbsp;texte&nbsp;était&nbsp;vrai&nbsp;en&nbsp;2002<br />//&nbsp;nous&nbsp;voulons&nbsp;le&nbsp;mettre&nbsp;a&nbsp;jour&nbsp;pour&nbsp;2003<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Le&nbsp;premier&nbsp;avril&nbsp;est&nbsp;le&nbsp;04/01/2002\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$text</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"Le&nbsp;dernier&nbsp;noël&nbsp;était&nbsp;le&nbsp;12/24/2001\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Fonction&nbsp;de&nbsp;callback<br /></span><span style="color: #007700">function&nbsp;</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 />&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;comme&nbsp;d'habitude&nbsp;:&nbsp;$matches[0]&nbsp;représente&nbsp;la&nbsp;valeur&nbsp;totale<br />&nbsp;&nbsp;//&nbsp;$matches[1]&nbsp;représente&nbsp;la&nbsp;première&nbsp;parenthèse&nbsp;capturante<br />&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</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&nbsp;</span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"|(\d{2}/\d{2}/)(\d{4})|"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"next_year"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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 para"><p>L&#039;exemple ci-dessus va afficher :</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Le premier avril est le 04/01/2003
Le dernier noël était le 12/24/2002
</pre></div>
    </div>
   </div><p>
  </p>
  <p class="para">
   </p><div class="example">
    <p><b>Exemple #3 Exemple avec <b>preg_replace_callback()</b> en utilisant une structure récursive
     pour gérer du BB code</b></p>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"plain&nbsp;[indent]&nbsp;deep&nbsp;[indent]&nbsp;deeper&nbsp;[/indent]&nbsp;deep&nbsp;[/indent]&nbsp;plain"</span><span style="color: #007700">;<br /><br />function&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$regex&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#'</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$input&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'&lt;div&nbsp;style="margin-left:&nbsp;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 />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">preg_replace_callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$regex</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'parseTagsRecursive'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$output&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</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>
  </p>
 </div>


 <a name="function.preg-replace-callback.seealso"></a><div class="refsect1 seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   </p><ul class="simplelist">
    <li class="member"><a href="function.preg-replace.php" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Rechercher et remplacer par expression rationnelle standard</li>
    <li class="member"><a href="function.create-function.php" class="function" rel="rdfs-seeAlso">create_function()</a> - Crée une fonction anonyme</li>
    <li class="member">Informations sur le type <a href="language.pseudo-types.php#language.types.callback" class="link">callback</a></li>
   </ul><p>
  </p>
 </div>


</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites