<?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 => 'de',
  ),
  'this' => 
  array (
    0 => 'function.str-replace.php',
    1 => 'str_replace',
    2 => 'Ersetzt alle Vorkommen des Suchstrings durch einen anderen String',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String-Funktionen',
  ),
  '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' => 'de',
    '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">Ersetzt alle Vorkommen des Suchstrings durch einen anderen String</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.str-replace-description">
  <h3 class="title">Beschreibung</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">
   Diese Funktion gibt einen String oder ein Array zurück, in dem alle
   Vorkommen von <code class="parameter">search</code> innerhalb von
   <code class="parameter">subject</code> durch den angegebenen
   <code class="parameter">replace</code>-Wert ersetzt wurden.
  </p>
  <p class="para">
   Um Text anhand eines Musters statt einer konkreten Zeichenkette zu
   ersetzen, kann <span class="function"><a href="function.preg-replace.php" class="function">preg_replace()</a></span> verwendet werden.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.str-replace-parameters">
  <h3 class="title">Parameter-Liste</h3>
  <p class="para">
   Sind <code class="parameter">search</code> und <code class="parameter">replace</code>
   Arrays, nimmt <span class="function"><strong>str_replace()</strong></span> je einen Wert beider Arrays
   und verwendet diese zum Suchen und Ersetzen in
   <code class="parameter">subject</code>. Hat <code class="parameter">replace</code> weniger
   Werte als <code class="parameter">search</code>, so wird ein leerer String zum
   Ersetzen für den Rest der Werte verwendet. Ist
   <code class="parameter">search</code> ein Array und <code class="parameter">replace</code>
   ein String, dann wird dieser String für jeden Wert von
   <code class="parameter">search</code> angewandt. Das Gegenteil ist nicht sinnvoll.
  </p>
  <p class="para">
   Sind <code class="parameter">search</code> und/oder <code class="parameter">replace</code>
   Arrays, werden ihre Elemente vom ersten zum letzten hin verarbeitet.
  </p>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">search</code></dt>
     <dd>
      <p class="para">
       Der gesuchte Wert, auch <em>Nadel (needle)</em> genannt.
       Ein Array kann genutzt werden, um mehrere Nadeln zu bestimmen.
      </p>
     </dd>
    
    
     <dt><code class="parameter">replace</code></dt>
     <dd>
      <p class="para">
       Der Ersetzungswert, der gefundene <code class="parameter">search</code> Werte
       ersetzt. Ein Array kann genutzt werden, um mehrere Werte zu bestimmen.
      </p>
     </dd>
    
    
     <dt><code class="parameter">subject</code></dt>
     <dd>
      <p class="para">
       Die zu durchsuchende und darin enthaltene zu ersetzende Zeichenkette
       oder das Array, auch <em>Heuhaufen (haystack)</em> genannt.
      </p>
      <p class="para">
       Ist <code class="parameter">subject</code> ein Array, erfolgt das Suchen und
       Ersetzen für jeden Wert von <code class="parameter">subject</code>, und der
       Rückgabewert ist ebenfalls ein Array.
      </p>
     </dd>
    
    
     <dt><code class="parameter">count</code></dt>
     <dd>
      <p class="para">
       Falls übergeben, wird dies auf die Anzahl der durchgeführten
       Ersetzungen gesetzt.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.str-replace-returnvalues">
  <h3 class="title">Rückgabewerte</h3>
  <p class="para">
   Diese Funktion gibt eine Zeichenkette oder ein Array mit ersetzten Werten
   zurück.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.str-replace-examples">
  <h3 class="title">Beispiele</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Beispiel #1 Grundlegende <span class="function"><strong>str_replace()</strong></span>-Beispiele</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">// Liefert: &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">// Liefert: Hll PHP-Wlt<br /></span><span style="color: #0000BB">$vokale </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">$nurkonsonanten </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$vokale</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">"Hallo PHP-Welt"</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$nurkonsonanten</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Liefert: Sie sollten taeglich Pizza, Bier und Eiscreme essen.<br /></span><span style="color: #0000BB">$satz </span><span style="color: #007700">= </span><span style="color: #DD0000">"Sie sollten taeglich Fruechte, Gemuese und Ballaststoffe essen."</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$gesund </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"Fruechte"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Gemuese"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Ballaststoffe"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$lecker </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"Pizza"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Bier"</span><span style="color: #007700">, </span><span style="color: #DD0000">"Eiscreme"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$neuersatz </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$gesund</span><span style="color: #007700">, </span><span style="color: #0000BB">$lecker</span><span style="color: #007700">, </span><span style="color: #0000BB">$satz</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$neuersatz</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Liefert: 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">$anzahl</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$anzahl</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>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>Beispiel #2 Beispiele möglicher Probleme durch <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">// Reihenfolge von Ersetzungen<br /></span><span style="color: #0000BB">$str     </span><span style="color: #007700">= </span><span style="color: #DD0000">"Zeile 1\nZeile 2\rZeile 3\r\nZeile 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 /></span><span style="color: #FF8000">// Verarbeitet \r\n's zuerst, so dass sie nicht doppelt konvertiert werden<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">// Ausgabe: F da A durch B ersetzt wird, dann B durch C, und so weiter...<br />// Letztlich wird E durch F ersetzt, wegen der Ersetzung von Links nach Rechts<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">// Ausgabe: apearpearle pear<br />// Aus dem gleichen Grund wie oben beschrieben<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">Anmerkungen</h3>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: <span class="simpara">Diese Funktion ist
binärsicher.</span></p></blockquote>
  <div class="caution"><strong class="caution">Achtung</strong>
   <h1 class="title">Mögliche Probleme durch Reihenfolge der Ersetzungen</h1>
   <p class="para">
    Weil <span class="function"><strong>str_replace()</strong></span> von links nach rechts ersetzt, kann
    ein zuvor eingesetzter Wert ersetzt werden, wenn mehrere Ersetzungen
    durchgeführt werden. Das letzte Beispiel zeigt, was dies für Auswirkungen
    hat.
   </p>
  </div>
  <blockquote class="note"><p><strong class="note">Hinweis</strong>: 
   <p class="para">
    Die Funktion berücksichtigt Groß- und Kleinschreibung. Verwenden Sie
    <span class="function"><a href="function.str-ireplace.php" class="function">str_ireplace()</a></span>, wenn Sie unabhängig von Groß- und
    Kleinschreibung ersetzen möchten.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.str-replace-seealso">
  <h3 class="title">Siehe auch</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> - Gro&szlig;- und kleinschreibungsunabh&auml;ngige Version von
   str_replace</span></li>
    <li><span class="function"><a href="function.substr-replace.php" class="function" rel="rdfs-seeAlso">substr_replace()</a> - Ersetzt Text innerhalb einer Zeichenkette</span></li>
    <li><span class="function"><a href="function.preg-replace.php" class="function" rel="rdfs-seeAlso">preg_replace()</a> - Sucht und ersetzt mit regul&auml;ren Ausdr&uuml;cken</span></li>
    <li><span class="function"><a href="function.strtr.php" class="function" rel="rdfs-seeAlso">strtr()</a> - Tauscht Zeichen aus oder ersetzt Zeichenketten</span></li>
   </ul>
  </p>
 </div>


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