<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Replace text within a portion of a string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.substr-count.html">substr_count</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.substr.html">substr</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.substr-replace" class="refentry"> <div class="refnamediv"> <h1 class="refname">substr_replace</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">substr_replace</span> — <span class="dc-title">Replace text within a portion of a string</span></p> </div> <div class="refsect1 description" id="refsect1-function.substr-replace-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>substr_replace</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$string</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$replacement</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$start</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$length</code></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>substr_replace()</strong></span> replaces a copy of <em><code class="parameter">string</code></em> delimited by the <em><code class="parameter">start</code></em> and (optionally) <em><code class="parameter">length</code></em> parameters with the string given in <em><code class="parameter">replacement</code></em>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.substr-replace-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">string</code></em></span> <dd> <p class="para"> The input string. </p> <p class="para"> An <span class="type"><a href="language.types.array.html" class="type array">array</a></span> of <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s can be provided, in which case the replacements will occur on each string in turn. In this case, the <em><code class="parameter">replacement</code></em>, <em><code class="parameter">start</code></em> and <em><code class="parameter">length</code></em> parameters may be provided either as scalar values to be applied to each input string in turn, or as <span class="type"><a href="language.types.array.html" class="type array">array</a></span>s, in which case the corresponding array element will be used for each input string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">replacement</code></em></span> <dd> <p class="para"> The replacement string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">start</code></em></span> <dd> <p class="para"> If <em><code class="parameter">start</code></em> is positive, the replacing will begin at the <em><code class="parameter">start</code></em>'th offset into <em><code class="parameter">string</code></em>. </p> <p class="para"> If <em><code class="parameter">start</code></em> is negative, the replacing will begin at the <em><code class="parameter">start</code></em>'th character from the end of <em><code class="parameter">string</code></em>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">length</code></em></span> <dd> <p class="para"> If given and is positive, it represents the length of the portion of <em><code class="parameter">string</code></em> which is to be replaced. If it is negative, it represents the number of characters from the end of <em><code class="parameter">string</code></em> at which to stop replacing. If it is not given, then it will default to strlen( <em><code class="parameter">string</code></em> ); i.e. end the replacing at the end of <em><code class="parameter">string</code></em>. Of course, if <em><code class="parameter">length</code></em> is zero then this function will have the effect of inserting <em><code class="parameter">replacement</code></em> into <em><code class="parameter">string</code></em> at the given <em><code class="parameter">start</code></em> offset. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.substr-replace-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The result string is returned. If <em><code class="parameter">string</code></em> is an array then array is returned. </p> </div> <div class="refsect1 changelog" id="refsect1-function.substr-replace-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.3.3</td> <td> All parameters now accept <span class="type"><a href="language.types.array.html" class="type array">array</a></span>s. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.substr-replace-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4915"> <p><strong>Example #1 Simple <span class="function"><strong>substr_replace()</strong></span> examples</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$var </span><span style="color: #007700">= </span><span style="color: #DD0000">'ABCDEFGH:/MNRPQR/'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">"Original: </span><span style="color: #0000BB">$var</span><span style="color: #DD0000"><hr />\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* These two examples replace all of $var with 'bob'. */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">, </span><span style="color: #DD0000">'bob'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">, </span><span style="color: #DD0000">'bob'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">)) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Insert 'bob' right at the beginning of $var. */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">, </span><span style="color: #DD0000">'bob'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* These next two replace 'MNRPQR' in $var with 'bob'. */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">, </span><span style="color: #DD0000">'bob'</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">, </span><span style="color: #DD0000">'bob'</span><span style="color: #007700">, -</span><span style="color: #0000BB">7</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* Delete 'MNRPQR' from $var. */<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">) . </span><span style="color: #DD0000">"<br />\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4916"> <p><strong>Example #2 Using <span class="function"><strong>substr_replace()</strong></span> to replace multiple strings at once </strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'A: XXX'</span><span style="color: #007700">, </span><span style="color: #DD0000">'B: XXX'</span><span style="color: #007700">, </span><span style="color: #DD0000">'C: XXX'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// A simple case: replace XXX in each string with YYY.<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">'; '</span><span style="color: #007700">, </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #DD0000">'YYY'</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: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// A more complicated case where each replacement is different.<br /></span><span style="color: #0000BB">$replace </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'AAA'</span><span style="color: #007700">, </span><span style="color: #DD0000">'BBB'</span><span style="color: #007700">, </span><span style="color: #DD0000">'CCC'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">'; '</span><span style="color: #007700">, </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">$replace</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: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Replace a different number of characters each time.<br /></span><span style="color: #0000BB">$length </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">'; '</span><span style="color: #007700">, </span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">, </span><span style="color: #0000BB">$replace</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">$length</span><span style="color: #007700">)).</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> A: YYY; B: YYY; C: YYY A: AAA; B: BBB; C: CCC A: AAAXX; B: BBBX; C: CCC </pre></div> </div> </div> </p> </div> <div class="refsect1 notes" id="refsect1-function.substr-replace-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is binary-safe.</span></p></blockquote> </div> <div class="refsect1 seealso" id="refsect1-function.substr-replace-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a> - Replace all occurrences of the search string with the replacement string</span></li> <li class="member"> <span class="function"><a href="function.substr.html" class="function" rel="rdfs-seeAlso">substr()</a> - Return part of a string</span></li> <li class="member"><a href="language.types.string.html#language.types.string.substr" class="link">String access and modification by character</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.substr-count.html">substr_count</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.substr.html">substr</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>