Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Regular Expression Functions (POSIX Extended) : ereg_replace

ereg_replace

Replace regular expression (PHP 4, PHP 5)
string ereg_replace ( string pattern, string replacement, string string )

Example 1891. 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);

?>

Example 1892. 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 1893. Replace URLs with links

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

Related Examples ( Source code ) » ereg_replace







Code Examples / Notes » ereg_replace

mephissto

Your right but you just need to replace by :
<?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);
}
?>


bgoodman

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


joachim kruyswijk

Use mb_ereg_replace() instead of ereg_replace() when working with multi-byte strings!

cristiklein

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
?>


benlanc

Quite how I managed to get my previous post so wrong, I don't know. Correction follows:
<?php
/* function to turn InterCaps style strings (i.e. CSS Properties in Javascript) to human readable ones */
function deInterCaps($var){
  return ucfirst(strtolower(ereg_replace("[A-Z]"," \\0",$var)));
}
$interCapsString = "aLoadOfNonsenseToDemonstrateTheFunction";
echo deInterCaps($interCapsString);
// output: A load of nonsense to demonstrate the function
?>


php dot net

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.
If you're ever having trouble with this one there's an easy workarround:
instead of
<?php
$foo = 23;
echo ereg_replace ( "bar", $foo , "foobar" );
?>
just do
<?php
$foo = 23;
echo ereg_replace ( "bar", "" . $foo , "foobar" );
?>
to replace "bar" inside "foobar" with the string "23".


bjwarshaw2

It's worth mentioning for ultimate clarity that you're safest using double quotes when matching a pattern, since without them, metacharacters will be interpreted as a backslash plus another character.
Granted, this is part of the language syntax for the string type, but it might not be quite so obvious when dealing with patterns in this function, which is taking the pattern as a parameter.
So if you find that '[\n]' is taking the 'n' out of your string and leaving the new lines alone, switch to doubles before changing anything else.


codergeek42

In response to "php dot net at lenix dot de," a cleaner (easier to read) method would be to type-cast the integer as a string by quoting it. For example:
<?php
$foo = 42;
echo ereg_replace ( "bar", "$foo" , "foobar" ); /* Would output "foo42". */
?>


zaczek

If you want the function to process query strings, such as:
http://www.php.net/index.php?id=10%32&wp=test
modify the function as follows:
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);
}


chris

I've updated the function a little that was posted below.  I use it to make database field names readable when making a header row.  I needed it to quit putting a space in "GPA" and to put a space in between numbers and letters.
<?php
/**
*  Converts "fieldcontainingGPAInMyDatabaseFrom2005"
*  To       "Field Containing GPA In My Database From 2005"
*/
function innerCapsToReadableText($text) {
$text = ereg_replace("([A-Z]) ", "\\1",ucwords(strtolower(ereg_replace("[A-Z]"," \\0",$text))));
return ereg_replace("([A-Za-z])([0-9])", "\\1 \\2", $text);
}
?>


chrish

I was having problems with Microsoft Outlook viewing forms within email.  I was only able to see the first word of the text box after I used the following code.
If I entered words into the text box and used the enter key to give me a CRLF I could see in the returned data the %0D%0A string, so I assumed if I just used the ereg-replace as below it would just replace the %0D%0A with a single space...
function remove_extra_linebreaks($string) {
  $new_string=ereg_replace("%0D%0A", " ", $string);
 return $new_string;
}
But the form as displayed by Outlook only showed the text upto the first replaced string, then it was blank!
I could view the source of the email and it would show all of the text I expected.
The following will show the correct data in the form
function remove_extra_linebreaks($string) {
  $new_string=ereg_replace("%0D%0A", '+', $string);
 return $new_string;
}


bmcswee

Function to strip an HTML tag out of a string. I use this in part for parsing XML documents.
<?php
function stripTags($tag, $string) {
       // Regular expressions only work with strings if the regexp has been pre-concocted
$regExp = "<" . "$tag" . "[^>]*>";
$string = str_replace("</$tag>", "", $string);
$string = ereg_replace($regExp, "", $string);

return $string;
}
?>


jarfil

For simple patterns like "[a-z]", preg_replace is up to 6 times faster than ereg_replace.

hoffaboffa

cristiklein's hyperlink function is nice but works incorrect with a www-string like this
\r\nwww.google.se
then it does not become a hyperlink


eerie

<?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.


Change Language


Follow Navioo On Twitter
ereg_replace
ereg
eregi_replace
eregi
split
spliti
sql_regcase
eXTReMe Tracker