📄 function.substr-replace.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Replace text within a portion of a string</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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"> <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"><b><b>substr_replace</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$string</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$replacement</tt></span> , <span class="methodparam"><span class="type">int</span> <tt class="parameter">$start</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$length</tt></span> ] )</div> <p class="para rdfs-comment"> <b>substr_replace()</b> replaces a copy of <i><tt class="parameter">string</tt></i> delimited by the <i><tt class="parameter">start</tt></i> and (optionally) <i><tt class="parameter">length</tt></i> parameters with the string given in <i><tt class="parameter">replacement</tt></i>. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">string</tt></i></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">replacement</tt></i></span> <dd> <p class="para"> The replacement string. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">start</tt></i></span> <dd> <p class="para"> If <i><tt class="parameter">start</tt></i> is positive, the replacing will begin at the <i><tt class="parameter">start</tt></i>'th offset into <i><tt class="parameter">string</tt></i>. </p> <p class="para"> If <i><tt class="parameter">start</tt></i> is negative, the replacing will begin at the <i><tt class="parameter">start</tt></i>'th character from the end of <i><tt class="parameter">string</tt></i>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">length</tt></i></span> <dd> <p class="para"> If given and is positive, it represents the length of the portion of <i><tt class="parameter">string</tt></i> which is to be replaced. If it is negative, it represents the number of characters from the end of <i><tt class="parameter">string</tt></i> at which to stop replacing. If it is not given, then it will default to strlen( <i><tt class="parameter">string</tt></i> ); i.e. end the replacing at the end of <i><tt class="parameter">string</tt></i>. Of course, if <i><tt class="parameter">length</tt></i> is zero then this function will have the effect of inserting <i><tt class="parameter">replacement</tt></i> into <i><tt class="parameter">string</tt></i> at the given <i><tt class="parameter">start</tt></i> offset. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> The result string is returned. If <i><tt class="parameter">string</tt></i> is an array then array is returned. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>substr_replace()</b> example</b></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: $var<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> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: <span class="simpara">This function isbinary-safe.</span></p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.str-replace.html" class="function" rel="rdfs-seeAlso">str_replace()</a></li> <li class="member"><a href="function.substr.html" class="function" rel="rdfs-seeAlso">substr()</a></li> </ul> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -