<HTML ><HEAD ><TITLE >substr_replace</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="String functions" HREF="ref.strings.html"><LINK REL="PREVIOUS" TITLE="substr_count" HREF="function.substr-count.html"><LINK REL="NEXT" TITLE="substr" HREF="function.substr.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation 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-count.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.substr.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.substr-replace" ></A >substr_replace</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN91727" ></A ><P > (PHP 4 )</P >substr_replace -- Replace text within a portion of a string</DIV ><DIV CLASS="refsect1" ><A NAME="AEN91730" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >substr_replace</B > ( string string, string replacement, int start [, int length])<BR ></BR ><P > <B CLASS="function" >substr_replace()</B > replaces a copy 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" ><A NAME="AEN91768" ></A ><P ><B >Example 1. <B CLASS="function" >substr_replace()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $var = 'ABCDEFGH:/MNRPQR/'; echo "Original: $var<hr>\n"; /* These two examples replace all of $var with 'bob'. */ echo substr_replace($var, 'bob', 0) . "<br>\n"; echo substr_replace($var, 'bob', 0, strlen($var)) . "<br>\n"; /* Insert 'bob' right at the beginning of $var. */ echo substr_replace($var, 'bob', 0, 0) . "<br>\n"; /* These next two replace 'MNRPQR' in $var with 'bob'. */ echo substr_replace($var, 'bob', 10, -1) . "<br>\n"; echo substr_replace($var, 'bob', -7, -1) . "<br>\n"; /* Delete 'MNRPQR' from $var. */ echo substr_replace($var, '', 10, -1) . "<br>\n"; ?></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 ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.substr-count.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.substr.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >substr_count</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >substr</TD ></TR ></TABLE ></DIV ></BODY ></HTML >