<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >preg_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="Regular Expression Functions (Perl-Compatible)" HREF="ref.pcre.html"><LINK REL="PREVIOUS" TITLE="preg_replace_callback" HREF="function.preg-replace-callback.html"><LINK REL="NEXT" TITLE="preg_split" HREF="function.preg-split.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.preg-replace-callback.html" ACCESSKEY="P" >Anterior</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.preg-split.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.preg-replace" ></A >preg_replace</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN167304" ></A ><P > (PHP 3 >= 3.0.9, PHP 4, PHP 5)</P >preg_replace -- Perform a regular expression search and replace</DIV ><DIV CLASS="refsect1" ><A NAME="AEN167307" ></A ><H2 >Descrição</H2 >mixed <B CLASS="methodname" >preg_replace</B > ( mixed pattern, mixed replacement, mixed subject [, int limit [, int &count]] )<BR ></BR ><P > Searches <CODE CLASS="parameter" >subject</CODE > for matches to <CODE CLASS="parameter" >pattern</CODE > and replaces them with <CODE CLASS="parameter" >replacement</CODE >. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167331" ></A ><H2 >Parâmetros</H2 ><P > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><CODE CLASS="parameter" >pattern</CODE ></DT ><DD ><P > The pattern to search for. It can be either a string or an array with strings. </P ><P > The <TT CLASS="literal" >e</TT > modifier makes <B CLASS="function" >preg_replace()</B > treat the <CODE CLASS="parameter" >replacement</CODE > parameter as PHP code after the appropriate references substitution is done. Tip: make sure that <CODE CLASS="parameter" >replacement</CODE > constitutes a valid PHP code string, otherwise PHP will complain about a parse error at the line containing <B CLASS="function" >preg_replace()</B >. </P ></DD ><DT ><CODE CLASS="parameter" >replacement</CODE ></DT ><DD ><P > The string or an array with strings to replace. If this parameter is a string and the <CODE CLASS="parameter" >pattern</CODE > parameter is an array, all patterns will be replaced by that string. If both <CODE CLASS="parameter" >pattern</CODE > and <CODE CLASS="parameter" >replacement</CODE > parameters are arrays, each <CODE CLASS="parameter" >pattern</CODE > will be replaced by the <CODE CLASS="parameter" >replacement</CODE > counterpart. If there are fewer elements in the <CODE CLASS="parameter" >replacement</CODE > array than in the <CODE CLASS="parameter" >pattern</CODE > array, any extra <CODE CLASS="parameter" >pattern</CODE >s will be replaced by an empty string. </P ><P > <CODE CLASS="parameter" >replacement</CODE > may contain references of the form <TT CLASS="literal" >\\<TT CLASS="replaceable" ><I >n</I ></TT ></TT > or (since PHP 4.0.4) <TT CLASS="literal" ><TT CLASS="replaceable" ><I >$n</I ></TT ></TT >, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the <TT CLASS="replaceable" ><I >n</I ></TT >'th parenthesized pattern. <TT CLASS="replaceable" ><I >n </I ></TT >can be from 0 to 99, and <TT CLASS="literal" >\\0</TT > or <TT CLASS="literal" >$0</TT > refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. </P ><P > When working with a replacement pattern where a backreference is immediately followed by another number (i.e.: placing a literal number immediately after a matched pattern), you cannot use the familiar <TT CLASS="literal" >\\1</TT > notation for your backreference. <TT CLASS="literal" >\\11</TT >, for example, would confuse <B CLASS="function" >preg_replace()</B > since it does not know whether you want the <TT CLASS="literal" >\\1</TT > backreference followed by a literal <TT CLASS="literal" >1</TT >, or the <TT CLASS="literal" >\\11</TT > backreference followed by nothing. In this case the solution is to use <TT CLASS="literal" >\${1}1</TT >. This creates an isolated <TT CLASS="literal" >$1</TT > backreference, leaving the <TT CLASS="literal" >1</TT > as a literal. </P ><P > When using the <TT CLASS="literal" >e</TT > modifier, this function escapes some characters (namely <TT CLASS="literal" >'</TT >, <TT CLASS="literal" >"</TT >, <TT CLASS="literal" >\</TT > and NULL) in the strings that replace the backreferences. This is done to ensure that no syntax errors arise from backreference usage with either single or double quotes (e.g. <TT CLASS="literal" >'strlen(\'$1\')+strlen("$2")'</TT >). Make sure you are aware of PHP's <A HREF="language.types.string.html" >string syntax</A > to know exactly how the interpreted string will look like. </P ></DD ><DT ><CODE CLASS="parameter" >subject</CODE ></DT ><DD ><P > The string or an array with strings to search and replace. </P ><P > If <CODE CLASS="parameter" >subject</CODE > is an array, then the search and replace is performed on every entry of <CODE CLASS="parameter" >subject</CODE >, and the return value is an array as well. </P ></DD ><DT ><CODE CLASS="parameter" >limit</CODE ></DT ><DD ><P > The maximum possible replacements for each pattern in each <CODE CLASS="parameter" >subject</CODE > string. Defaults to <TT CLASS="literal" >-1</TT > (no limit). </P ></DD ><DT ><CODE CLASS="parameter" >count</CODE ></DT ><DD ><P > If specified, this variable will be filled with the number of replacements done. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167406" ></A ><H2 >Valores de retornado</H2 ><P > <B CLASS="function" >preg_replace()</B > returns an array if the <CODE CLASS="parameter" >subject</CODE > parameter is an array, or a string otherwise. </P ><P > If matches are found, the new <CODE CLASS="parameter" >subject</CODE > will be returned, otherwise <CODE CLASS="parameter" >subject</CODE > will be returned unchanged. </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167414" ></A ><H2 >Histórico</H2 ><P > <DIV CLASS="informaltable" ><P ></P ><A NAME="AEN167417" ></A ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Versão</TH ><TH >Descrição</TH ></TR ></THEAD ><TBODY ><TR ><TD >5.1.0</TD ><TD > Added the <CODE CLASS="parameter" >count</CODE > parameter </TD ></TR ><TR ><TD >4.0.4</TD ><TD > Added the '$n' form for the <CODE CLASS="parameter" >replacement</CODE > parameter </TD ></TR ><TR ><TD >4.0.1</TD ><TD > Added the <CODE CLASS="parameter" >limit</CODE > parameter </TD ></TR ></TBODY ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167436" ></A ><H2 >Exemplos</H2 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN167439" ></A ><P ><B >Exemplo 1. Using backreferences followed by numeric literals</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$string </font><font color="#007700">= </font><font color="#DD0000">'April 15, 2003'</font><font color="#007700">;<br /></font><font color="#0000BB">$pattern </font><font color="#007700">= </font><font color="#DD0000">'/(\w+) (\d+), (\d+)/i'</font><font color="#007700">;<br /></font><font color="#0000BB">$replacement </font><font color="#007700">= </font><font color="#DD0000">'${1}1,$3'</font><font color="#007700">;<br />echo </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#0000BB">$pattern</font><font color="#007700">, </font><font color="#0000BB">$replacement</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >April1,2003</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN167445" ></A ><P ><B >Exemplo 2. Using indexed arrays with <B CLASS="function" >preg_replace()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$string </font><font color="#007700">= </font><font color="#DD0000">'The quick brown fox jumped over the lazy dog.'</font><font color="#007700">;<br /></font><font color="#0000BB">$patterns</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">] = </font><font color="#DD0000">'/quick/'</font><font color="#007700">;<br /></font><font color="#0000BB">$patterns</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">] = </font><font color="#DD0000">'/brown/'</font><font color="#007700">;<br /></font><font color="#0000BB">$patterns</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">] = </font><font color="#DD0000">'/fox/'</font><font color="#007700">;<br /></font><font color="#0000BB">$replacements</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">] = </font><font color="#DD0000">'bear'</font><font color="#007700">;<br /></font><font color="#0000BB">$replacements</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">] = </font><font color="#DD0000">'black'</font><font color="#007700">;<br /></font><font color="#0000BB">$replacements</font><font color="#007700">[</font><font color="#0000BB">0</font><font color="#007700">] = </font><font color="#DD0000">'slow'</font><font color="#007700">;<br />echo </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#0000BB">$patterns</font><font color="#007700">, </font><font color="#0000BB">$replacements</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >The bear black slow jumped over the lazy dog.</PRE ></TD ></TR ></TABLE ><P > By ksorting patterns and replacements, we should get what we wanted. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />ksort</font><font color="#007700">(</font><font color="#0000BB">$patterns</font><font color="#007700">);<br /></font><font color="#0000BB">ksort</font><font color="#007700">(</font><font color="#0000BB">$replacements</font><font color="#007700">);<br />echo </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#0000BB">$patterns</font><font color="#007700">, </font><font color="#0000BB">$replacements</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >The slow black bear jumped over the lazy dog.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN167456" ></A ><P ><B >Exemplo 3. Replacing several values</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$patterns </font><font color="#007700">= array (</font><font color="#DD0000">'/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/'</font><font color="#007700">,<br /> </font><font color="#DD0000">'/^\s*{(\w+)}\s*=/'</font><font color="#007700">);<br /></font><font color="#0000BB">$replace </font><font color="#007700">= array (</font><font color="#DD0000">'\3/\4/\1\2'</font><font color="#007700">, </font><font color="#DD0000">'$\1 ='</font><font color="#007700">);<br />echo </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#0000BB">$patterns</font><font color="#007700">, </font><font color="#0000BB">$replace</font><font color="#007700">, </font><font color="#DD0000">'{startDate} = 1999-5-27'</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >$startDate = 5/27/1999</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN167462" ></A ><P ><B >Exemplo 4. Using the 'e' modifier</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />preg_replace</font><font color="#007700">(</font><font color="#DD0000">"/(<\/?)(\w+)([^>]*>)/e"</font><font color="#007700">, <br /> </font><font color="#DD0000">"'\\1'.strtoupper('\\2').'\\3'"</font><font color="#007700">, <br /> </font><font color="#0000BB">$html_body</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > This would capitalize all HTML tags in the input text. </P ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN167467" ></A ><P ><B >Exemplo 5. Strip whitespace</B ></P ><P > This example strips excess whitespace from a string. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$str </font><font color="#007700">= </font><font color="#DD0000">'foo o'</font><font color="#007700">;<br /></font><font color="#0000BB">$str </font><font color="#007700">= </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#DD0000">'/\s\s+/'</font><font color="#007700">, </font><font color="#DD0000">' '</font><font color="#007700">, </font><font color="#0000BB">$str</font><font color="#007700">);<br /></font><font color="#FF8000">// This will be 'foo o' now<br /></font><font color="#007700">echo </font><font color="#0000BB">$str</font><font color="#007700">;<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN167472" ></A ><P ><B >Exemplo 6. Using the <CODE CLASS="parameter" >count</CODE > parameter</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$count </font><font color="#007700">= </font><font color="#0000BB">0</font><font color="#007700">;<br /><br />echo </font><font color="#0000BB">preg_replace</font><font color="#007700">(array(</font><font color="#DD0000">'/\d/'</font><font color="#007700">, </font><font color="#DD0000">'/\s/'</font><font color="#007700">), </font><font color="#DD0000">'*'</font><font color="#007700">, </font><font color="#DD0000">'xp 4 to'</font><font color="#007700">, -</font><font color="#0000BB">1 </font><font color="#007700">, </font><font color="#0000BB">$count</font><font color="#007700">);<br />echo </font><font color="#0000BB">$count</font><font color="#007700">; </font><font color="#FF8000">//3<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P >O exemplo acima irá imprimir:</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >xp***to 3</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167478" ></A ><H2 >Notas</H2 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Nota: </B > When using arrays with <CODE CLASS="parameter" >pattern</CODE > and <CODE CLASS="parameter" >replacement</CODE >, the keys are processed in the order they appear in the array. This is <SPAN CLASS="emphasis" ><I CLASS="emphasis" >not necessarily</I ></SPAN > the same as the numerical index order. If you use indexes to identify which <CODE CLASS="parameter" >pattern</CODE > should be replaced by which <CODE CLASS="parameter" >replacement</CODE >, you should perform a <A HREF="function.ksort.html" ><B CLASS="function" >ksort()</B ></A > on each array prior to calling <B CLASS="function" >preg_replace()</B >. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="refsect1" ><A NAME="AEN167489" ></A ><H2 >Veja também</H2 ><P > <P ></P ><TABLE BORDER="0" ><TBODY ><TR ><TD ><A HREF="function.preg-match.html" ><B CLASS="function" >preg_match()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.preg-replace-callback.html" ><B CLASS="function" >preg_replace_callback()</B ></A ></TD ></TR ><TR ><TD ><A HREF="function.preg-split.html" ><B CLASS="function" >preg_split()</B ></A ></TD ></TR ></TBODY ></TABLE ><P ></P > </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.preg-replace-callback.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.preg-split.html" ACCESSKEY="N" >Próxima</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >preg_replace_callback</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.pcre.html" ACCESSKEY="U" >Acima</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >preg_split</TD ></TR ></TABLE ></DIV ></BODY ></HTML >