<?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.str-replace.php',
    1 => 'str_replace',
    2 => 'Remplace toutes les occurrences 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.str-repeat.php',
    1 => 'str_repeat',
  ),
  'next' => 
  array (
    0 => 'function.str-rot13.php',
    1 => 'str_rot13',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'reference/strings/functions/str-replace.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.str-replace" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">str_replace</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">str_replace</span> &mdash; <span class="dc-title">Remplace toutes les occurrences dans une chaîne</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.str-replace-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>str_replace</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$search</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$replace</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 reference">&$count</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></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></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>str_replace()</strong></span> retourne une chaîne ou un tableau,
   dont toutes les occurrences de <code class="parameter">search</code> dans
   <code class="parameter">subject</code> ont été remplacées par
   <code class="parameter">replace</code>.
  </p>
  <p class="para">
   Pour remplacer un texte en fonction d&#039;un modèle plutôt qu&#039;une chaîne fixe,
   utiliser <span class="function"><a href="function.preg-replace.php" class="function">preg_replace()</a></span>.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.str-replace-parameters">
  <h3 class="title">Liste de paramètres</h3>
  <p class="para">
   Si les paramètres <code class="parameter">search</code> et <code class="parameter">replace</code>
   sont des tableaux, alors la fonction <span class="function"><strong>str_replace()</strong></span>
   prendra une valeur de chaque tableau et les utilisera pour la recherche et
   le remplacement sur <code class="parameter">subject</code>. Si le paramètre
   <code class="parameter">replace</code> a moins de valeurs que le paramètre
   <code class="parameter">search</code>, alors une <a href="language.types.string.php" class="link">chaîne de caractères</a> vide sera utilisée
   comme valeur pour le reste des valeurs de remplacement. Si le paramètre
   <code class="parameter">search</code> est un tableau et que le paramètre
   <code class="parameter">replace</code> est une <a href="language.types.string.php" class="link">chaîne de caractères</a>, alors cette <a href="language.types.string.php" class="link">chaîne de caractères</a>
   de remplacement sera utilisée pour chaque valeur de <code class="parameter">search</code>.
   L&#039;inverse n&#039;a pas de sens.
  </p>
  <p class="para">
   Si <code class="parameter">search</code> ou <code class="parameter">replace</code>
   sont des tableaux, les éléments sont traités du premier, au dernier.
  </p>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">search</code></dt>
     <dd>
      <p class="para">
       La valeur à chercher, autrement connue comme le <em>masque</em>.
       Un tableau peut être utilisé pour désigner plusieurs masques.
      </p>
     </dd>
    
    
     <dt><code class="parameter">replace</code></dt>
     <dd>
      <p class="para">
       La valeur de remplacement à substituer aux valeurs trouvées.
       Un tableau peut être utilisé pour désigner plusieurs valeurs
       de remplacement.
      </p>
     </dd>
    
    
     <dt><code class="parameter">subject</code></dt>
     <dd>
      <p class="para">
       La <a href="language.types.string.php" class="link">chaîne de caractères</a> ou le tableau sur lequel on va effectuer la
       recherche et le remplacement, aussi connu sous le nom
       de <em>haystack</em>.
      </p>
      <p class="para">
       Si <code class="parameter">subject</code> est un tableau, alors le
       remplacement se fera sur chaque élément de celui-ci, et
       la valeur retournée sera aussi un tableau.
      </p>
     </dd>
    
    
     <dt><code class="parameter">count</code></dt>
     <dd>
      <p class="para">
       Si fournie, cette variable contiendra le nombre de remplacements effectués.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.str-replace-returnvalues">
  <h3 class="title">Valeurs de retour</h3>
  <p class="para">
   Cette fonction retourne une chaîne, ou un tableau, contenant les valeurs
   remplacées.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.str-replace-examples">
  <h3 class="title">Exemples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Exemple #1 Exemple 1 avec <span class="function"><strong>str_replace()</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">// Génère : &lt;body text='black'&gt;<br /></span><span style="color: #0000BB">$bodytag </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"%body%"</span><span style="color: #007700">, </span><span style="color: #DD0000">"black"</span><span style="color: #007700">, </span><span style="color: #DD0000">"&lt;body text='%body%'&gt;"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$bodytag</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Génère : Hll Wrld f PHP<br /></span><span style="color: #0000BB">$vowels </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"a"</span><span style="color: #007700">, </span><span style="color: #DD0000">"e"</span><span style="color: #007700">, </span><span style="color: #DD0000">"i"</span><span style="color: #007700">, </span><span style="color: #DD0000">"o"</span><span style="color: #007700">, </span><span style="color: #DD0000">"u"</span><span style="color: #007700">, </span><span style="color: #DD0000">"A"</span><span style="color: #007700">, </span><span style="color: #DD0000">"E"</span><span style="color: #007700">, </span><span style="color: #DD0000">"I"</span><span style="color: #007700">, </span><span style="color: #DD0000">"O"</span><span style="color: #007700">, </span><span style="color: #DD0000">"U"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$onlyconsonants </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$vowels</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"Hello World of PHP"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$onlyconsonants</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Génère : You should eat pizza, beer, and ice cream every day<br /></span><span style="color: #0000BB">$phrase  </span><span style="color: #007700">= </span><span style="color: #DD0000">"You should eat fruits, vegetables, and fiber every day."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$healthy </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"fruits"</span><span style="color: #007700">, </span><span style="color: #DD0000">"vegetables"</span><span style="color: #007700">, </span><span style="color: #DD0000">"fiber"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$yummy   </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"pizza"</span><span style="color: #007700">, </span><span style="color: #DD0000">"beer"</span><span style="color: #007700">, </span><span style="color: #DD0000">"ice cream"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$newphrase </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$healthy</span><span style="color: #007700">, </span><span style="color: #0000BB">$yummy</span><span style="color: #007700">, </span><span style="color: #0000BB">$phrase</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$newphrase</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Génère : 2<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"ll"</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"good golly miss molly!"</span><span style="color: #007700">, </span><span style="color: #0000BB">$count</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$count</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</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 Exemple 2 avec <span class="function"><strong>str_replace()</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">// Ordre des remplacements<br /></span><span style="color: #0000BB">$str     </span><span style="color: #007700">= </span><span style="color: #DD0000">"Line 1\nLine 2\rLine 3\r\nLine 4\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$order   </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"\r\n"</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">, </span><span style="color: #DD0000">"\r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$replace </span><span style="color: #007700">= </span><span style="color: #DD0000">'&lt;br /&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Traitement du premier \r\n, ils ne seront pas convertis deux fois.<br /></span><span style="color: #0000BB">$newstr </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$order</span><span style="color: #007700">, </span><span style="color: #0000BB">$replace</span><span style="color: #007700">, </span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$newstr</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 F car A est remplacé par B, puis B est remplacé par C, et ainsi de suite...<br />// Finalement, E est remplacé par F<br /></span><span style="color: #0000BB">$search  </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'A'</span><span style="color: #007700">, </span><span style="color: #DD0000">'B'</span><span style="color: #007700">, </span><span style="color: #DD0000">'C'</span><span style="color: #007700">, </span><span style="color: #DD0000">'D'</span><span style="color: #007700">, </span><span style="color: #DD0000">'E'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$replace </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'B'</span><span style="color: #007700">, </span><span style="color: #DD0000">'C'</span><span style="color: #007700">, </span><span style="color: #DD0000">'D'</span><span style="color: #007700">, </span><span style="color: #DD0000">'E'</span><span style="color: #007700">, </span><span style="color: #DD0000">'F'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$subject </span><span style="color: #007700">= </span><span style="color: #DD0000">'A'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$search</span><span style="color: #007700">, </span><span style="color: #0000BB">$replace</span><span style="color: #007700">, </span><span style="color: #0000BB">$subject</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 : apearpearle pear<br />// Pour les mêmes raisons que plus haut<br /></span><span style="color: #0000BB">$letters </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'p'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$fruit   </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">, </span><span style="color: #DD0000">'pear'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$text    </span><span style="color: #007700">= </span><span style="color: #DD0000">'a p'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$output  </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$letters</span><span style="color: #007700">, </span><span style="color: #0000BB">$fruit</span><span style="color: #007700">, </span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$output</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 notes" id="refsect1-function.str-replace-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">
 Cette fonction gère les chaînes binaires.
</span></p></blockquote>
  <div class="caution"><strong class="caution">Attention</strong>
   <h1 class="title">Ordre de remplacement</h1>
   <p class="para">
    À cause du fait que la fonction <span class="function"><strong>str_replace()</strong></span>
    effectue les remplacements de la gauche vers la droite, elle peut
    remplacer une valeur précédemment insérée lors de multiples
    remplacements.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Cette fonction est sensible à la casse. Utiliser la fonction
    <span class="function"><a href="function.str-ireplace.php" class="function">str_ireplace()</a></span> pour un remplacement insensible à la
    casse.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.str-replace-seealso">
  <h3 class="title">Voir aussi</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.str-ireplace.php" class="function" rel="rdfs-seeAlso">str_ireplace()</a> - Version insensible &agrave; la casse de str_replace</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-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.strtr.php" class="function" rel="rdfs-seeAlso">strtr()</a> - Remplace des caract&egrave;res dans une cha&icirc;ne</span></li>
   </ul>
  </p>
 </div>


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