|
|
|
Description
</H2
><DIV
CLASS="funcsynopsis"
><A
NAME="AEN37197"
></A
><P
></P
><P
><CODE
><CODE
CLASS="FUNCDEF"
>string <B
CLASS="function"
>ereg_replace</B
></CODE
> (string pattern, string replacement, string string)</CODE
></P
><P
></P
></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="AEN37222"
></A
><P
><B
>Example 1. <B
CLASS="function"
>Ereg_replace()</B
> Example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><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="AEN37229"
></A
><P
><B
>Example 2. <B
CLASS="function"
>ereg_replace()</B
> Example</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> <
/* 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
>
See also <A
HREF="get_example.php3?Function=ereg"
><B
CLASS="function"
>ereg()</B
></A
>, <A
HREF="get_example.php3?Function=eregi"
><B
CLASS="function"
>eregi()</B
></A
>,
and <A
HREF="get_example.php3?Function=eregi-replace"
><B
CLASS="function"
>eregi_replace()</B
></A
>.
|
|
| PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | This script is a contact form between users of a
website (kinda like the PM function on the forums)
Categories : PHP, Databases, MySQL, Regexps | | | Simple way to replace a variable value in a .conf (.ini) file using a
webbrowser - the first stage of a complete universal configuration editor Categories : PHP, Regexps, Code Editors, Filesystem | | | ereg -- Regular expression match Categories : PHP, PHP Functions, Regexps | | | Gets the browser and OS from the $_SERVER['http_user_agent'] variable in PHP Categories : PHP, HTTP, Regexps | | | I need a trim function/regexp that will trim all " " from the ends of a string. Categories : Regexps, PHP, Strings | | | Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays | | | Form input return conformance Categories : HTML and PHP, PHP, Regexps | | | Simple PHP program which calls other PHP program you can pass the
variables to other PHP program : by Raju Categories : PHP, PHP Options and Info, Regexps, Program Execution | | | How to build a search query for any N number of words in a search string Categories : PHP, Regexps, Search Engines, Search | | | Link Extractor - This function is used to extract links from a given URL. This will convert relative path into absolute path and also remove PHPSESSID stuff. Categories : PHP, URLs, Regexps | | | Newbie Notes #7 - Ridiculous regex Categories : PHP, Beginner Guides, Regexps | | | email validator check checker email e-mail email address Categories : PHP, Email, Regexps | | | An array of functions to use in checking user input to HTML forms : text, firstName, middleNameOrInit, lastName, email, web, digits, decimal, hex, genNum, USD, BPS, Euro, USphone, USzip Categories : PHP, Data Validation, Regexps | |
|
|
|