<HTML ><HEAD ><TITLE >ereg_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 (POSIX Extended)" HREF="ref.regex.html"><LINK REL="PREVIOUS" TITLE="Regular Expression Functions (POSIX Extended)" HREF="ref.regex.html"><LINK REL="NEXT" TITLE="ereg" HREF="function.ereg.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="ref.regex.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.ereg.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.ereg-replace" ></A >ereg_replace</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN81962" ></A ><P > (PHP 3, PHP 4 )</P >ereg_replace -- Replace regular expression</DIV ><DIV CLASS="refsect1" ><A NAME="AEN81965" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >ereg_replace</B > ( string pattern, string replacement, string string)<BR ></BR ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > <A HREF="function.preg-replace.html" ><B CLASS="function" >preg_replace()</B ></A >, which uses a Perl-compatible regular expression syntax, is often a faster alternative to <B CLASS="function" >ereg_replace()</B >. </P ></BLOCKQUOTE ></DIV ><P > This function scans <TT CLASS="parameter" ><I >string</I ></TT > for matches to <TT CLASS="parameter" ><I >pattern</I ></TT >, then replaces the matched text with <TT CLASS="parameter" ><I >replacement</I ></TT >. </P ><P > The modified string is returned. (Which may mean that the original string is returned if there are no matches to be replaced.) </P ><P > If <TT CLASS="parameter" ><I >pattern</I ></TT > contains parenthesized substrings, <TT CLASS="parameter" ><I >replacement</I ></TT > may contain substrings of the form <TT CLASS="literal" >\\<TT CLASS="replaceable" ><I >digit</I ></TT ></TT >, which will be replaced by the text matching the digit'th parenthesized substring; <TT CLASS="literal" >\\0</TT > will produce the entire contents of string. Up to nine substrings may be used. Parentheses may be nested, in which case they are counted by the opening parenthesis. </P ><P > If no matches are found in <TT CLASS="parameter" ><I >string</I ></TT >, then <TT CLASS="parameter" ><I >string</I ></TT > will be returned unchanged. </P ><P > For example, the following code snippet prints "This was a test" three times: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN81998" ></A ><P ><B >Example 1. <B CLASS="function" >ereg_replace()</B > Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >$string = "This is a test"; echo ereg_replace (" is", " was", $string); echo ereg_replace ("( )is", "\\1was", $string); echo ereg_replace ("(( )is)", "\\2was", $string);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > One thing to take note of is that if you use an integer value as the <TT CLASS="parameter" ><I >replacement</I ></TT > parameter, you may not get the results you expect. This is because <B CLASS="function" >ereg_replace()</B > will interpret the number as the ordinal value of a character, and apply that. For instance: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN82005" ></A ><P ><B >Example 2. <B CLASS="function" >ereg_replace()</B > Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><?php /* This will not work as expected. */ $num = 4; $string = "This string has four words."; $string = ereg_replace('four', $num, $string); echo $string; /* Output: 'This string has words.' */ /* This will work. */ $num = '4'; $string = "This string has four words."; $string = ereg_replace('four', $num, $string); echo $string; /* Output: 'This string has 4 words.' */ ?></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="AEN82010" ></A ><P ><B >Example 3. Replace URLs with links</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", "<a href=\"\\0\">\\0</a>", $text);</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also <A HREF="function.ereg.html" ><B CLASS="function" >ereg()</B ></A >, <A HREF="function.eregi.html" ><B CLASS="function" >eregi()</B ></A >, <A HREF="function.eregi-replace.html" ><B CLASS="function" >eregi_replace()</B ></A >, <A HREF="function.str-replace.html" ><B CLASS="function" >str_replace()</B ></A >, and <A HREF="function.preg-match.html" ><B CLASS="function" >preg_match()</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="ref.regex.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.ereg.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Regular Expression Functions (POSIX Extended)</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.regex.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >ereg</TD ></TR ></TABLE ></DIV ></BODY ></HTML >