<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >preg_replace</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual 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=ISO-8859-2"></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 PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.preg-replace-callback.html" ACCESSKEY="P" >Înapoi</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" >Înainte</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="AEN100185" ></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="AEN100188" ></A ><H2 >Description</H2 >mixed <B CLASS="methodname" >preg_replace</B > ( mixed pattern, mixed replacement, mixed subject [, int limit])<BR ></BR ><P > Searches <VAR CLASS="parameter" >subject</VAR > for matches to <VAR CLASS="parameter" >pattern</VAR > and replaces them with <VAR CLASS="parameter" >replacement</VAR >. If <VAR CLASS="parameter" >limit</VAR > is specified, then only <VAR CLASS="parameter" >limit</VAR > matches will be replaced; if <VAR CLASS="parameter" >limit</VAR > is omitted or is -1, then all matches are replaced. </P ><P > <VAR CLASS="parameter" >Replacement</VAR > may contain references of the form <VAR CLASS="literal" >\\<VAR CLASS="replaceable" >n</VAR ></VAR > or (since PHP 4.0.4) <VAR CLASS="literal" ><VAR CLASS="replaceable" >$n</VAR ></VAR >, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the <VAR CLASS="replaceable" >n</VAR >'th parenthesized pattern. <VAR CLASS="replaceable" >n </VAR >can be from 0 to 99, and <VAR CLASS="literal" >\\0</VAR > or <VAR CLASS="literal" >$0</VAR > 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 <VAR CLASS="literal" >\\1</VAR > notation for your backreference. <VAR CLASS="literal" >\\11</VAR >, for example, would confuse <B CLASS="function" >preg_replace()</B > since it does not know whether you want the <VAR CLASS="literal" >\\1</VAR > backreference followed by a literal <VAR CLASS="literal" >1</VAR >, or the <VAR CLASS="literal" >\\11</VAR > backreference followed by nothing. In this case the solution is to use <VAR CLASS="literal" >\${1}1</VAR >. This creates an isolated <VAR CLASS="literal" >$1</VAR > backreference, leaving the <VAR CLASS="literal" >1</VAR > as a literal. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN100233" ></A ><P ><B >Exemplu 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 > This example will output : </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 > If matches are found, the new <VAR CLASS="parameter" >subject</VAR > will be returned, otherwise <VAR CLASS="parameter" >subject</VAR > will be returned unchanged. </P ><P > Every parameter to <B CLASS="function" >preg_replace()</B > (except <VAR CLASS="parameter" >limit</VAR >) can be an unidimensional array. When using arrays with <VAR CLASS="parameter" >pattern</VAR > and <VAR CLASS="parameter" >replacement</VAR >, 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 <VAR CLASS="parameter" >pattern</VAR > should be replaced by which <VAR CLASS="parameter" >replacement</VAR >, 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 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN100252" ></A ><P ><B >Exemplu 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 /><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 /><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 /><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 > Output: </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 /><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 /><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 /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P > Output : </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 > If <VAR CLASS="parameter" >subject</VAR > is an array, then the search and replace is performed on every entry of <VAR CLASS="parameter" >subject</VAR >, and the return value is an array as well. </P ><P > If <VAR CLASS="parameter" >pattern</VAR > and <VAR CLASS="parameter" >replacement</VAR > are arrays, then <B CLASS="function" >preg_replace()</B > takes a value from each array and uses them to do search and replace on <VAR CLASS="parameter" >subject</VAR >. If <VAR CLASS="parameter" >replacement</VAR > has fewer values than <VAR CLASS="parameter" >pattern</VAR >, then empty string is used for the rest of replacement values. If <VAR CLASS="parameter" >pattern </VAR > is an array and <VAR CLASS="parameter" >replacement</VAR > is a string, then this replacement string is used for every value of <VAR CLASS="parameter" >pattern</VAR >. The converse would not make sense, though. </P ><P > <VAR CLASS="literal" >/e</VAR > modifier makes <B CLASS="function" >preg_replace()</B > treat the <VAR CLASS="parameter" >replacement</VAR > parameter as PHP code after the appropriate references substitution is done. Tip: make sure that <VAR CLASS="parameter" >replacement</VAR > 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 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN100282" ></A ><P ><B >Exemplu 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">"</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 > This example will produce: </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="AEN100288" ></A ><P ><B >Exemplu 4. Using /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="AEN100293" ></A ><P ><B >Exemplu 5. Convert HTML to text</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// $document should contain an HTML document.<br />// This will remove HTML tags, javascript sections<br />// and white space. It will also convert some<br />// common HTML entities to their text equivalent.<br /><br /></font><font color="#0000BB">$search </font><font color="#007700">= array (</font><font color="#DD0000">"'<script[^>]*?>.*?</script>'si"</font><font color="#007700">, </font><font color="#FF8000">// Strip out javascript<br /> </font><font color="#DD0000">"'<[\/\!]*?[^<>]*?>'si"</font><font color="#007700">, </font><font color="#FF8000">// Strip out HTML tags<br /> </font><font color="#DD0000">"'([\r\n])[\s]+'"</font><font color="#007700">, </font><font color="#FF8000">// Strip out white space<br /> </font><font color="#DD0000">"'&(quot|#34);'i"</font><font color="#007700">, </font><font color="#FF8000">// Replace HTML entities<br /> </font><font color="#DD0000">"'&(amp|#38);'i"</font><font color="#007700">,<br /> </font><font color="#DD0000">"'&(lt|#60);'i"</font><font color="#007700">,<br /> </font><font color="#DD0000">"'&(gt|#62);'i"</font><font color="#007700">,<br /> </font><font color="#DD0000">"'&(nbsp|#160);'i"</font><font color="#007700">,<br /> </font><font color="#DD0000">"'&(iexcl|#161);'i"</font><font color="#007700">,<br /> </font><font color="#DD0000">"'&(cent|#162);'i"</font><font color="#007700">,<br /> </font><font color="#DD0000">"'&(pound|#163);'i"</font><font color="#007700">,<br /> </font><font color="#DD0000">"'&(copy|#169);'i"</font><font color="#007700">,<br /> </font><font color="#DD0000">"'&#(\d+);'e"</font><font color="#007700">); </font><font color="#FF8000">// evaluate as php<br /><br /></font><font color="#0000BB">$replace </font><font color="#007700">= array (</font><font color="#DD0000">""</font><font color="#007700">,<br /> </font><font color="#DD0000">""</font><font color="#007700">,<br /> </font><font color="#DD0000">"\\1"</font><font color="#007700">,<br /> </font><font color="#DD0000">"\""</font><font color="#007700">,<br /> </font><font color="#DD0000">"&"</font><font color="#007700">,<br /> </font><font color="#DD0000">"<"</font><font color="#007700">,<br /> </font><font color="#DD0000">">"</font><font color="#007700">,<br /> </font><font color="#DD0000">" "</font><font color="#007700">,<br /> </font><font color="#0000BB">chr</font><font color="#007700">(</font><font color="#0000BB">161</font><font color="#007700">),<br /> </font><font color="#0000BB">chr</font><font color="#007700">(</font><font color="#0000BB">162</font><font color="#007700">),<br /> </font><font color="#0000BB">chr</font><font color="#007700">(</font><font color="#0000BB">163</font><font color="#007700">),<br /> </font><font color="#0000BB">chr</font><font color="#007700">(</font><font color="#0000BB">169</font><font color="#007700">),<br /> </font><font color="#DD0000">"chr(\\1)"</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$text </font><font color="#007700">= </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#0000BB">$search</font><font color="#007700">, </font><font color="#0000BB">$replace</font><font color="#007700">, </font><font color="#0000BB">$document</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B > Parameter <VAR CLASS="parameter" >limit</VAR > was added after PHP 4.0.1pl2. </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.preg-match.html" ><B CLASS="function" >preg_match()</B ></A >, <A HREF="function.preg-match-all.html" ><B CLASS="function" >preg_match_all()</B ></A >, and <A HREF="function.preg-split.html" ><B CLASS="function" >preg_split()</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.preg-replace-callback.html" ACCESSKEY="P" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.preg-split.html" ACCESSKEY="N" >Înainte</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" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >preg_split</TD ></TR ></TABLE ></DIV ></BODY ></HTML >