WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : ereg_replace -- Replace regular expression
Categories : PHP, PHP Functions, Regexps Update Picture
documentation group PHP
Date : Apr 24th 1999
Grade : Be the 1st to grade this Code Example
Viewed : 4004
File : No file for this code example.
Images : No Images for this code example.
Search : More code by documentation group PHP
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

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
>&#13; 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
>&#13; The modified string is returned. (Which may mean that the
original string is returned if there are no matches to be
replaced.)
</P
><P
>&#13; 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
>&#13; 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
>&#13; 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"
>&#13;$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
>&#13; 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"
>&#13;&#60;
/* 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.' */
?&#62;
</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
</P
><P
>&#13;

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