PHP ereg_replace Function


PHP ereg_replace() is a deprecated function. It replaces the specific pattern matches inside a string using regular expression. ereg_replace() function is much slower than other string functions such as str_replace() which do not support regular expressions.

<?PHP
$str="2000 East Whatson Rd";
$ret=ereg_replace("\d+","",$str);
echo "$ret";  //East Whatson Rd
?>

For special characters of a string, including " ? . * etc, a "\" is needed to put infront of it when included in a pattern.
<?PHP
$str="2000 East? Whatson Rd";
$ret=ereg_replace("\?","",$str);
echo "$ret";  //2000 East Whatson Rd
?>

ereg_replace() function can reserve the string pattern at a specificed position in the new string using "( )".
<?PHP
$str="2000 East? Whatson Rd";
$ret=ereg_replace("\s(East)\?"," - 3000 \\1",$str);
echo "$ret";  //2000 - 3000 East Whatson Rd
?>


Similiar functions include ereg(), preg_match(), preg_replace(), str_replace().