<HTML ><HEAD ><TITLE >substr_replace</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.44"><LINK REL="HOME" TITLE="PHP Manual" HREF="manual.html"><LINK REL="UP" TITLE="String functions" HREF="ref.strings.html"><LINK REL="PREVIOUS" TITLE="substr" HREF="function.substr.html"><LINK REL="NEXT" TITLE="trim" HREF="function.trim.html"></HEAD ><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.substr.html" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.trim.html" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.substr-replace" >substr_replace</A ></H1 ><DIV CLASS="refnamediv" ><A NAME="AEN26810" ></A >substr_replace -- Replace text within a portion of a string.</DIV ><DIV CLASS="refsect1" ><A NAME="AEN26813" ></A ><H2 >Description</H2 ><DIV CLASS="funcsynopsis" ><P ></P ><CODE CLASS="FUNCDEF" >string substr_replace</CODE >(string string, string replacement, int start, int [<SPAN CLASS="optional" >length</SPAN >] );<P ></P ></DIV ><P > <B CLASS="function" >substr_replace()</B > replaces the part of <TT CLASS="parameter" ><I >string</I ></TT > delimited by the <TT CLASS="parameter" ><I >start</I ></TT > and (optionally) <TT CLASS="parameter" ><I >length</I ></TT > parameters with the string given in <TT CLASS="parameter" ><I >replacement</I ></TT >. The result is returned. </P ><P > If <TT CLASS="parameter" ><I >start</I ></TT > is positive, the replacing will begin at the <TT CLASS="parameter" ><I >start</I ></TT >'th offset into <TT CLASS="parameter" ><I >string</I ></TT >. </P ><P > If <TT CLASS="parameter" ><I >start</I ></TT > is negative, the replacing will begin at the <TT CLASS="parameter" ><I >start</I ></TT >'th character from the end of <TT CLASS="parameter" ><I >string</I ></TT >. </P ><P > If <TT CLASS="parameter" ><I >length</I ></TT > is given and is positive, it represents the length of the portion of <TT CLASS="parameter" ><I >string</I ></TT > which is to be replaced. If it is negative, it represents the number of characters from the end of <TT CLASS="parameter" ><I >string</I ></TT > at which to stop replacing. If it is not given, then it will default to strlen( <TT CLASS="parameter" ><I >string</I ></TT > ); i.e. end the replacing at the end of <TT CLASS="parameter" ><I >string</I ></TT >. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><P ><B >Example 1. <B CLASS="function" >Substr_replace()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="programlisting" > 1 2 <?php 3 $var = 'ABCDEFGH:/MNRPQR/'; 4 echo "Original: $var<hr>\n"; 5 6 /* These two examples replace all of $var with 'bob'. */ 7 echo substr_replace ($var, 'bob', 0) . "<br>\n"; 8 echo substr_replace ($var, 'bob', 0, strlen ($var)) . "<br>\n"; 9 10 /* Insert 'bob' right at the beginning of $var. */ 11 echo substr_replace ($var, 'bob', 0, 0) . "<br>\n"; 12 13 /* These next two replace 'MNRPQR' in $var with 'bob'. */ 14 echo substr_replace ($var, 'bob', 10, -1) . "<br>\n"; 15 echo substr_replace ($var, 'bob', -7, -1) . "<br>\n"; 16 17 /* Delete 'MNRPQR' from $var. */ 18 echo substr_replace ($var, '', 10, -1) . "<br>\n"; 19 ?> 20 </PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.str-replace.html" ><B CLASS="function" >str_replace()</B ></A > and <A HREF="function.substr.html" ><B CLASS="function" >substr()</B ></A >. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > <B CLASS="function" >Substr_replace()</B > was added in PHP 4.0. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.substr.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="manual.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.trim.html" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >substr</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >trim</TD ></TR ></TABLE ></DIV ></BODY ></HTML >