search for in the  
<POSIX Regexereg>
Last updated: Thu, 19 May 2005

ereg_replace

(PHP 3, PHP 4, PHP 5)

ereg_replace -- Replace regular expression

Description

string ereg_replace ( string pattern, string replacement, string string )

This function scans string for matches to pattern, then replaces the matched text with replacement.

The modified string is returned. (Which may mean that the original string is returned if there are no matches to be replaced.)

If pattern contains parenthesized substrings, replacement may contain substrings of the form \\digit, which will be replaced by the text matching the digit'th parenthesized substring; \\0 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.

If no matches are found in string, then string will be returned unchanged.

For example, the following code snippet prints "This was a test" three times:

Example 1. ereg_replace() example

<?php

$string
= "This is a test";
echo
str_replace(" is", " was", $string);
echo
ereg_replace("( )is", "\\1was", $string);
echo
ereg_replace("(( )is)", "\\2was", $string);

?>

One thing to take note of is that if you use an integer value as the replacement parameter, you may not get the results you expect. This is because ereg_replace() will interpret the number as the ordinal value of a character, and apply that. For instance:

Example 2. ereg_replace() example

<?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.' */
?>

Example 3. Replace URLs with links

<?php
$text
= ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]",
                    
"<a href=\"\\0\">\\0</a>", $text);
?>

Tip: preg_replace(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg_replace().

See also ereg(), eregi(), eregi_replace(), str_replace(), and preg_match().



User Contributed Notes
ereg_replace
Sanitization Function
10-Apr-2005 08:09
// Clean potentially nasty stuff from a string
function clean_string($string)
{
   return ereg_replace("[^[:space:]a-zA-Z0-9*_.-]", "", $string);
}
cristiklein at yahoo dot com
09-Apr-2005 04:50
Sometimes, you would like to match both styles of URL links that are common in chat windows:

http://www.yahoo.com
www.yahoo.com

You can do this by using the following code:

<?php
function hyperlink(&$text)
{
  
// match protocol://address/path/
  
$text = ereg_replace("[a-zA-Z]+://([.]?[a-zA-Z0-9_/-])*", "<a href=\"\\0\">\\0</a>", $text);

  
// match www.something
  
$text = ereg_replace("(^| )(www([.]?[a-zA-Z0-9_/-])*)", "\\1<a href=\"http://\\2\">\\2</a>", $text);
}
?>

You can use this function like this:
<?php
$line
= "Check the links: www.yahoo.com http://www.php.net";
hyperlink($line);
// Now, all text that looks like a link becomes a link
?>
bgoodman at osogrande dot com
02-Mar-2005 04:25
When you are dealing with databases you can end up with quite a few \" to deal with.  To ereg_replace all these with something else it requires you to \ the \ and \ the " so you end up with:

$var1 = '\"';
$var2 = ereg_replace('\\\"','1234',$var1);

print $var2;  //this should print 1234
eerie at gmx dot net
27-Feb-2005 01:44
<?php $path = ereg_replace("\\", "/", $path); ?>
as posted from mmtach at yahoo dot com causes an error because you have to escape the backslash twice, once for the quotation marks and a second time due the posix syntax.
<?php $path = ereg_replace("\\\\", "/", $path); ?>
or
<?php $path = ereg_replace('\\', "/", $path); ?>
should both work as expected. since you don't have to escape the backslash in brackets (posix syntax) his alternative works also.
AT-HE ( at_he at h0tmail dot com )
21-Feb-2005 05:08
hi..  i am just learning php since a few days.. i had mount a website in my own pc and want let users to post messages..

anyway, i wrote a very tiny page that let you run some commands remotely, that is useful mostly as example of ereg_replace and shell_exec :D

<!-----run.php-----
<pre>
<?php

$salida
=ereg_replace("<","&lt;",shell_exec($_POST["comando"]));
echo
"comando: ",$_POST["comando"],"<br>",$salida,"<br>";

?>
<form action="run.php" method="post">
comando: <input type="text" name="comando">
</form>

</pre>
-----eof----->

the ereg_replace is only for replace the "<" character at the output for his code, which html reads as a tag..

- first tou must write something in the form..
- when page is opened again it takes it, replaces "<" characters and puts in $salida variable (it may be useful later)..
-  then prints out and you can write something again :)

.. i haven't change the ">" and other characters.. i don't know if you can process the same string changing its lenght in the process..

maybe it becomes illegible after changing patterns of diffrent length such $str=ereg_replace("a","aaaaa",$str), dont know yet.

greetings ;)
mmtach at yahoo dot com
14-Apr-2002 06:04
found problem:
$path = ereg_replace("\\", "/", $path); //dos path to unix
gives warning error: "EREG_EESCAPE"
this does work:
$path = ereg_replace("[\\]", "/", $path);

<POSIX Regexereg>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT