<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >substr_replace</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="Manual do PHP" HREF="index.html"><LINK REL="UP" TITLE="String" 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=UTF-8"></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" >Manual do PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.substr-count.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.substr.html" ACCESSKEY="N" >Próxima</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="AEN221961" ></A ><P > (PHP 4, PHP 5)</P >substr_replace -- Substitui o texto dentro de uma parte de uma string</DIV ><DIV CLASS="refsect1" ><A NAME="AEN221964" ></A ><H2 >Descrição</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 > substitui uma cópia de <CODE CLASS="parameter" >string</CODE > delimitada pelos parâmetros <CODE CLASS="parameter" >start</CODE > e (opcionalmente) <CODE CLASS="parameter" >length</CODE > com a string dada em <CODE CLASS="parameter" >replacement</CODE >. O resultado é retornado. </P ><P > Se <CODE CLASS="parameter" >start</CODE > é positivo, a substituição começará no <CODE CLASS="parameter" >start</CODE >-ésimo caractere da <CODE CLASS="parameter" >string</CODE >. </P ><P > Se <CODE CLASS="parameter" >start</CODE > é negativo, a substituição começará no <CODE CLASS="parameter" >start</CODE >-ésimo caractere do final de <CODE CLASS="parameter" >string</CODE >. </P ><P > Se o <CODE CLASS="parameter" >length</CODE > é dado e é positivo, ele representa o comprimento da porção de <CODE CLASS="parameter" >string</CODE > que é para ser substituída. Se ele é negativo, ele representa o número de caracteres do final de <CODE CLASS="parameter" >string</CODE > para parar de substituir. Se ele não é dado, então o padrão será até strlen( <CODE CLASS="parameter" >string</CODE > ); i.e. o fim da substituição no final de <CODE CLASS="parameter" >string</CODE >. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN222002" ></A ><P ><B >Exemplo 1. <B CLASS="function" >substr_replace()</B > exemplo</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$var </font><font color="#007700">= </font><font color="#DD0000">'ABCDEFGH:/MNRPQR/'</font><font color="#007700">;<br />echo </font><font color="#DD0000">"Original: $var<hr></font><font color="#007700">\n</font><font color="#DD0000">"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">/* Estes dois exemplos substituem tudo de $var com 'bob'. */<br /></font><font color="#007700">echo </font><font color="#0000BB">substr_replace</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">, </font><font color="#DD0000">'bob'</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">) . </font><font color="#DD0000">"<br>\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">substr_replace</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">, </font><font color="#DD0000">'bob'</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">strlen</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">)) . </font><font color="#DD0000">"<br>\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">/* Insere 'bob' direto no começo de $var. */<br /></font><font color="#007700">echo </font><font color="#0000BB">substr_replace</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">, </font><font color="#DD0000">'bob'</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">, </font><font color="#0000BB">0</font><font color="#007700">) . </font><font color="#DD0000">"<br>\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">/* Estes dois exemplos substituem 'MNRPQR' em $var com 'bob'. */<br /></font><font color="#007700">echo </font><font color="#0000BB">substr_replace</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">, </font><font color="#DD0000">'bob'</font><font color="#007700">, </font><font color="#0000BB">10</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">) . </font><font color="#DD0000">"<br>\n"</font><font color="#007700">;<br />echo </font><font color="#0000BB">substr_replace</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">, </font><font color="#DD0000">'bob'</font><font color="#007700">, -</font><font color="#0000BB">7</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">) . </font><font color="#DD0000">"<br>\n"</font><font color="#007700">;<br /><br /></font><font color="#FF8000">/* Deleta 'MNRPQR' de $var. */<br /></font><font color="#007700">echo </font><font color="#0000BB">substr_replace</font><font color="#007700">(</font><font color="#0000BB">$var</font><font color="#007700">, </font><font color="#DD0000">''</font><font color="#007700">, </font><font color="#0000BB">10</font><font color="#007700">, -</font><font color="#0000BB">1</font><font color="#007700">) . </font><font color="#DD0000">"<br>\n"</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > Veja também<A HREF="function.str-replace.html" ><B CLASS="function" >str_replace()</B ></A > e <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" >Anterior</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Principal</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.substr.html" ACCESSKEY="N" >Próxima</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" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >substr</TD ></TR ></TABLE ></DIV ></BODY ></HTML >