<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'function.substr-replace.php',
    1 => 'substr_replace',
    2 => 'Replace text within a portion of a string',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String Функції',
  ),
  'prev' => 
  array (
    0 => 'function.substr-count.php',
    1 => 'substr_count',
  ),
  'next' => 
  array (
    0 => 'function.trim.php',
    1 => 'trim',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/strings/functions/substr-replace.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.substr-replace" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">substr_replace</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">substr_replace</span> &mdash; <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">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>substr_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">$string</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.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$offset</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.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.null.php" class="type null">null</a></span></span> <code class="parameter">$length</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>substr_replace()</strong></span> replaces a copy of
   <code class="parameter">string</code> delimited by the
   <code class="parameter">offset</code> and (optionally)
   <code class="parameter">length</code> parameters with the string given in
   <code class="parameter">replace</code>. 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.substr-replace-parameters">
  <h3 class="title">Параметри</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string</code></dt>
     <dd>
      <p class="para">
       The input string.
      </p>
      <p class="para">
       An <span class="type"><a href="language.types.array.php" class="type array">array</a></span> of <span class="type"><a href="language.types.string.php" 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 <code class="parameter">replace</code>, <code class="parameter">offset</code>
       and <code class="parameter">length</code> 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.php" class="type array">array</a></span>s, in which case the corresponding array element will
       be used for each input string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">replace</code></dt>
     <dd>
      <p class="para">
       The replacement string.
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       If <code class="parameter">offset</code> is non-negative, the replacing will
       begin at the <code class="parameter">offset</code>&#039;th offset into
       <code class="parameter">string</code>.
      </p>
      <p class="para">
       If <code class="parameter">offset</code> is negative, the replacing will
       begin at the <code class="parameter">offset</code>&#039;th character from the
       end of <code class="parameter">string</code>.
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       If given and is positive, it represents the length of the portion of
       <code class="parameter">string</code> which is to be replaced. If it is
       negative, it represents the number of characters from the end of
       <code class="parameter">string</code> at which to stop replacing. If it
       is not given, then it will default to strlen(
       <code class="parameter">string</code> ); i.e. end the replacing at the
       end of <code class="parameter">string</code>. Of course, if
       <code class="parameter">length</code> is zero then this function will have the
       effect of inserting <code class="parameter">replace</code> into
       <code class="parameter">string</code> at the given
       <code class="parameter">offset</code> offset.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.substr-replace-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="para">
   The result string is returned. If <code class="parameter">string</code> is an
   array then array is returned.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.substr-replace-changelog">
  <h3 class="title">Журнал змін</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Версія</th>
       <th>Опис</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        <code class="parameter">length</code> is nullable now.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.substr-replace-examples">
  <h3 class="title">Приклади</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Приклад #1 Simple <span class="function"><strong>substr_replace()</strong></span> examples</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">&lt;hr /&gt;\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">"&lt;br /&gt;\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">"&lt;br /&gt;\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">"&lt;br /&gt;\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">"&lt;br /&gt;\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">"&lt;br /&gt;\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">"&lt;br /&gt;\n"</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>Приклад #2 
     Using <span class="function"><strong>substr_replace()</strong></span> to replace multiple strings at
     once
    </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">= 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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">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">Примітки</h3>
  <blockquote class="note"><p><strong class="note">Зауваження</strong>: <span class="simpara">Ця функція є бінарно
безпечною.</span></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.substr-replace-seealso">
  <h3 class="title">Прогляньте також</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.str-replace.php" class="function" rel="rdfs-seeAlso">str_replace()</a> - Replace all occurrences of the search string with the replacement string</span></li>
    <li><span class="function"><a href="function.substr.php" class="function" rel="rdfs-seeAlso">substr()</a> - Return part of a string</span></li>
    <li><a href="language.types.string.php#language.types.string.substr" class="link">String access and modification by character</a></li>
   </ul>
  </p>
 </div>


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