<HTML ><HEAD ><TITLE >preg_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="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-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.preg-replace-callback.html" ACCESSKEY="P" >Prev</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" >Next</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="AEN81662" ></A ><P > (PHP 3>= 3.0.9, PHP 4 )</P >preg_replace -- Perform a regular expression search and replace</DIV ><DIV CLASS="refsect1" ><A NAME="AEN81665" ></A ><H2 >Description</H2 >mixed <B CLASS="methodname" >preg_replace</B > ( mixed pattern, mixed replacement, mixed subject [, int limit])<BR ></BR ><P > Searches <TT CLASS="parameter" ><I >subject</I ></TT > for matches to <TT CLASS="parameter" ><I > pattern</I ></TT > and replaces them with <TT CLASS="parameter" ><I >replacement</I ></TT >. If <TT CLASS="parameter" ><I >limit</I ></TT > is specified, then only <TT CLASS="parameter" ><I >limit</I ></TT > matches will be replaced; if <TT CLASS="parameter" ><I >limit</I ></TT > is omitted or is -1, then all matches are replaced. </P ><P > <TT CLASS="parameter" ><I >Replacement</I ></TT > 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. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > 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 ></BLOCKQUOTE ></DIV > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81710" ></A ><P ><B >Example 1. Using backreferences followed by numeric literals.</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $str = "April 15, 2003"; $pattern = "/(\w+) (\d+), (\d+)/i"; $replacement = "\${1}1,\$3"; print preg_replace($pattern, $replacement, $string); /* Output ====== April1,2003 */ ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > If matches are found, the new <TT CLASS="parameter" ><I >subject</I ></TT > will be returned, otherwise <TT CLASS="parameter" ><I >subject</I ></TT > will be returned unchanged. </P ><P > Every parameter to <B CLASS="function" >preg_replace()</B > (except <TT CLASS="parameter" ><I >limit</I ></TT >) can be an array. </P ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > When using arrays with <TT CLASS="parameter" ><I >pattern</I ></TT > and <TT CLASS="parameter" ><I >replacement</I ></TT >, 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 <TT CLASS="parameter" ><I >pattern</I ></TT > should be replaced by which <TT CLASS="parameter" ><I >replacement</I ></TT >, 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 > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81729" ></A ><P ><B >Example 2. Using indexed arrays with <B CLASS="function" >preg_replace()</B ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $string = "The quick brown fox jumped over the lazy dog."; $patterns[0] = "/quick/"; $patterns[1] = "/brown/"; $patterns[2] = "/fox/"; $replacements[2] = "bear"; $replacements[1] = "black"; $replacements[0] = "slow"; print preg_replace($patterns, $replacements, $string); /* Output ====== The bear black slow jumped over the lazy dog. */ /* By ksorting patterns and replacements, we should get what we wanted. */ ksort($patterns); ksort($replacements); print preg_replace($patterns, $replacements, $string); /* Output ====== The slow black bear jumped over the lazy dog. */ ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > If <TT CLASS="parameter" ><I >subject</I ></TT > is an array, then the search and replace is performed on every entry of <TT CLASS="parameter" ><I >subject</I ></TT >, and the return value is an array as well. </P ><P > If <TT CLASS="parameter" ><I >pattern</I ></TT > and <TT CLASS="parameter" ><I >replacement</I ></TT > are arrays, then <B CLASS="function" >preg_replace()</B > takes a value from each array and uses them to do search and replace on <TT CLASS="parameter" ><I >subject</I ></TT >. If <TT CLASS="parameter" ><I >replacement</I ></TT > has fewer values than <TT CLASS="parameter" ><I >pattern</I ></TT >, then empty string is used for the rest of replacement values. If <TT CLASS="parameter" ><I >pattern </I ></TT > is an array and <TT CLASS="parameter" ><I >replacement</I ></TT > is a string, then this replacement string is used for every value of <TT CLASS="parameter" ><I >pattern</I ></TT >. The converse would not make sense, though. </P ><P > <TT CLASS="literal" >/e</TT > modifier makes <B CLASS="function" >preg_replace()</B > treat the <TT CLASS="parameter" ><I >replacement</I ></TT > parameter as PHP code after the appropriate references substitution is done. Tip: make sure that <TT CLASS="parameter" ><I >replacement</I ></TT > 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="AEN81753" ></A ><P ><B >Example 3. Replacing several values</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >$patterns = array ("/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s*{(\w+)}\s*=/"); $replace = array ("\\3/\\4/\\1\\2", "$\\1 ="); print preg_replace ($patterns, $replace, "{startDate} = 1999-5-27");</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > This example will produce: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >$startDate = 5/27/1999</PRE ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81757" ></A ><P ><B >Example 4. Using /e modifier</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >preg_replace ("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtoupper('\\2').'\\3'", $html_body);</PRE ></TD ></TR ></TABLE ><P > This would capitalize all HTML tags in the input text. </P ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81761" ></A ><P ><B >Example 5. Convert HTML to text</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >// $document should contain an HTML document. // This will remove HTML tags, javascript sections // and white space. It will also convert some // common HTML entities to their text equivalent. $search = array ("'<script[^>]*?>.*?</script>'si", // Strip out javascript "'<[\/\!]*?[^<>]*?>'si", // Strip out html tags "'([\r\n])[\s]+'", // Strip out white space "'&(quot|#34);'i", // Replace html entities "'&(amp|#38);'i", "'&(lt|#60);'i", "'&(gt|#62);'i", "'&(nbsp|#160);'i", "'&(iexcl|#161);'i", "'&(cent|#162);'i", "'&(pound|#163);'i", "'&(copy|#169);'i", "'&#(\d+);'e"); // evaluate as php $replace = array ("", "", "\\1", "\"", "&", "<", ">", " ", chr(161), chr(162), chr(163), chr(169), "chr(\\1)"); $text = preg_replace ($search, $replace, $document);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Parameter <TT CLASS="parameter" ><I >limit</I ></TT > 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" >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.preg-split.html" ACCESSKEY="N" >Next</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" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >preg_split</TD ></TR ></TABLE ></DIV ></BODY ></HTML >