<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >preg_replace_callback</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_quote" HREF="function.preg-quote.html"><LINK REL="NEXT" TITLE="preg_replace" HREF="function.preg-replace.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-quote.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-replace.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.preg-replace-callback" ></A >preg_replace_callback</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN100143" ></A ><P > (PHP 4 >= 4.0.5, PHP 5)</P >preg_replace_callback -- Perform a regular expression search and replace using a callback</DIV ><DIV CLASS="refsect1" ><A NAME="AEN100146" ></A ><H2 >Description</H2 >mixed <B CLASS="methodname" >preg_replace_callback</B > ( mixed pattern, callback callback, mixed subject [, int limit])<BR ></BR ><P > The behavior of this function is almost identical to <A HREF="function.preg-replace.html" ><B CLASS="function" >preg_replace()</B ></A >, except for the fact that instead of <VAR CLASS="parameter" >replacement</VAR > parameter, one should specify a <VAR CLASS="parameter" >callback</VAR > that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN100167" ></A ><P ><B >Exemplu 1. <B CLASS="function" >preg_replace_callback()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /> </font><font color="#FF8000">// this text was used in 2002<br /> // we want to get this up to date for 2003<br /> </font><font color="#0000BB">$text </font><font color="#007700">= </font><font color="#DD0000">"April fools day is 04/01/2002\n"</font><font color="#007700">;<br /> </font><font color="#0000BB">$text</font><font color="#007700">.= </font><font color="#DD0000">"Last christmas was 12/24/2001\n"</font><font color="#007700">;<br /><br /> </font><font color="#FF8000">// the callback function<br /> </font><font color="#007700">function </font><font color="#0000BB">next_year</font><font color="#007700">(</font><font color="#0000BB">$matches</font><font color="#007700">) <br /> {<br /> </font><font color="#FF8000">// as usual: $matches[0] is the complete match<br /> // $matches[1] the match for the first subpattern<br /> // enclosed in '(...)' and so on<br /> </font><font color="#007700">return </font><font color="#0000BB">$matches</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">].(</font><font color="#0000BB">$matches</font><font color="#007700">[</font><font color="#0000BB">2</font><font color="#007700">]+</font><font color="#0000BB">1</font><font color="#007700">);<br /> }<br /><br /> echo </font><font color="#0000BB">preg_replace_callback</font><font color="#007700">(<br /> </font><font color="#DD0000">"|(\d{2}/\d{2}/)(\d{4})|"</font><font color="#007700">,<br /> </font><font color="#DD0000">"next_year"</font><font color="#007700">,<br /> </font><font color="#0000BB">$text</font><font color="#007700">);<br /><br /> </font><font color="#FF8000">// result is:<br /> // April fools day is 04/01/2003<br /> // Last christmas was 12/24/2002<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > You'll often need the <VAR CLASS="parameter" >callback</VAR > function for a <B CLASS="function" >preg_replace_callback()</B > in just one place. In this case you can use <A HREF="function.create-function.html" ><B CLASS="function" >create_function()</B ></A > to declare an anonymous function as callback within the call to <B CLASS="function" >preg_replace_callback()</B >. By doing it this way you have all information for the call in one place and do not clutter the function namespace with a callback functions name not used anywhere else. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN100176" ></A ><P ><B >Exemplu 2. <B CLASS="function" >preg_replace_callback()</B > and <A HREF="function.create-function.html" ><B CLASS="function" >create_function()</B ></A ></B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /> </font><font color="#FF8000">/* a unix-style command line filter to convert uppercase<br /> * letters at the beginning of paragraphs to lowercase */<br /><br /> </font><font color="#0000BB">$fp </font><font color="#007700">= </font><font color="#0000BB">fopen</font><font color="#007700">(</font><font color="#DD0000">"php://stdin"</font><font color="#007700">, </font><font color="#DD0000">"r"</font><font color="#007700">) or die(</font><font color="#DD0000">"can't read stdin"</font><font color="#007700">);<br /> while (!</font><font color="#0000BB">feof</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">)) {<br /> </font><font color="#0000BB">$line </font><font color="#007700">= </font><font color="#0000BB">fgets</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /> </font><font color="#0000BB">$line </font><font color="#007700">= </font><font color="#0000BB">preg_replace_callback</font><font color="#007700">(<br /> </font><font color="#DD0000">'|<p>\s*\w|'</font><font color="#007700">,<br /> </font><font color="#0000BB">create_function</font><font color="#007700">(<br /> </font><font color="#FF8000">// single quotes are essential here,<br /> // or alternative escape all $ as \$<br /> </font><font color="#DD0000">'$matches'</font><font color="#007700">,<br /> </font><font color="#DD0000">'return strtolower($matches[0]);'<br /> </font><font color="#007700">),<br /> </font><font color="#0000BB">$line<br /> </font><font color="#007700">);<br /> echo </font><font color="#0000BB">$line</font><font color="#007700">;<br /> }<br /> </font><font color="#0000BB">fclose</font><font color="#007700">(</font><font color="#0000BB">$fp</font><font color="#007700">);<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > See also <A HREF="function.preg-replace.html" ><B CLASS="function" >preg_replace()</B ></A > and <A HREF="function.create-function.html" ><B CLASS="function" >create_function()</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-quote.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-replace.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >preg_quote</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_replace</TD ></TR ></TABLE ></DIV ></BODY ></HTML >