Sophie

Sophie

distrib > Mageia > 7 > aarch64 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 6661

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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, PHP 7)</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">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
   <code class="parameter">string</code> delimited by the
   <code class="parameter">start</code> and (optionally)
   <code class="parameter">length</code> parameters with the string given in
   <code class="parameter">replacement</code>. 
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.substr-replace-parameters">
  <h3 class="title">Parameters</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.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 <code class="parameter">replacement</code>, <code class="parameter">start</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.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>
<code class="parameter">replacement</code></dt>

     <dd>

      <p class="para">
       The replacement string.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">start</code></dt>

     <dd>

      <p class="para">
       If <code class="parameter">start</code> is non-negative, the replacing will
       begin at the <code class="parameter">start</code>&#039;th offset into
       <code class="parameter">string</code>.
      </p>
      <p class="para">
       If <code class="parameter">start</code> is negative, the replacing will
       begin at the <code class="parameter">start</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">replacement</code> into
       <code class="parameter">string</code> at the given
       <code class="parameter">start</code> offset.
      </p>
     </dd>

    
   </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 <code class="parameter">string</code> is an
   array then array is returned.
  </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-6123">
    <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">&lt;?php<br />$var&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'ABCDEFGH:/MNRPQR/'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">"Original:&nbsp;</span><span style="color: #0000BB">$var</span><span style="color: #DD0000">&lt;hr&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;These&nbsp;two&nbsp;examples&nbsp;replace&nbsp;all&nbsp;of&nbsp;$var&nbsp;with&nbsp;'bob'.&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">))&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Insert&nbsp;'bob'&nbsp;right&nbsp;at&nbsp;the&nbsp;beginning&nbsp;of&nbsp;$var.&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;These&nbsp;next&nbsp;two&nbsp;replace&nbsp;'MNRPQR'&nbsp;in&nbsp;$var&nbsp;with&nbsp;'bob'.&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bob'</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">7</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Delete&nbsp;'MNRPQR'&nbsp;from&nbsp;$var.&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$var</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&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-6124">
    <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">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'A:&nbsp;XXX'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'B:&nbsp;XXX'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'C:&nbsp;XXX'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;A&nbsp;simple&nbsp;case:&nbsp;replace&nbsp;XXX&nbsp;in&nbsp;each&nbsp;string&nbsp;with&nbsp;YYY.<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">';&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'YYY'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</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">//&nbsp;A&nbsp;more&nbsp;complicated&nbsp;case&nbsp;where&nbsp;each&nbsp;replacement&nbsp;is&nbsp;different.<br /></span><span style="color: #0000BB">$replace&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'AAA'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'BBB'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'CCC'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">';&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replace</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</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">//&nbsp;Replace&nbsp;a&nbsp;different&nbsp;number&nbsp;of&nbsp;characters&nbsp;each&nbsp;time.<br /></span><span style="color: #0000BB">$length&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">';&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">substr_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$replace</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</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>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>