PHP | ereg_replace() Function
Last Updated :
11 Jul, 2025
The ereg_replace() is an inbuilt function in PHP and is used to search a string pattern in an other string. If pattern is found in the original string then it will replace matching text with a replacement string. You may refer to the article on Regular Expression for basic understanding of pattern matching using regular expressions.
Syntax:
string ereg_replace ( $string_pattern, $replace_string, $original_string )
Parameters Used: This function accepts three mandatory parameters and all of these parameters are described below.
- $string_pattern: This parameter specifies the pattern to be searched in the $original_string. Its can be used with both array and string type which is parenthesized substrings.
- $replace_string: This parameter specifies the string by which the matching text will be replaced and it can be used with both array and string type. The replacement contain substring in the form of \digit, which replaces the text matching digit'th parenthesized substring and \0 produce entire contents string.
- $original_string: This parameter specifies the input string and can be of both array and string type.
Return Value: This function returns a modified string or array if matches found. If matches not found in the original string then it will return unchanged original string or array.
Note: The ereg_replace() function is case-sensitive in PHP. This function was deprecated in PHP 5.3.0, and removed in PHP 7.0.0.
Examples:
Input: $original_string = "Geeksforgeeks PHP article.";
$string_pattern = "(.*)PHP(.*)";
$replace_string = " You should read \\1all\\2";
Output: You should read Geeksforgeeks all article.
Explanation: Within the parenthesis "\1" and "\2" to access
the part of string and replace with 'PHP' to 'all'.
Input: $original_string = "Geeksforgeeks is no:one computer
science portal.";
$replace_string = '1';
$original_string = ereg_replace('one', $replace_string,
$original_string);
Output: Geeksforgeeks is no:1 computer science portal.
The below programs illustrate the ereg_replace() function.
Program 1:
php
<?php
// Original input string
$original_string = "Write any topic .";
// Pattern to be searched
$string_pattern = "(.*)any(.*)";
// Replace string
$replace_string = " own yours own \\1biography\\2";
echo ereg_replace($patternstrVal, $replacesstrVal, $stringVal);
?>
Output:
Write own yours own biography topic.
Note: While using an integer value as the replacement parameter, we do not get expected result as the function interpret the number to ordinal value of character.
Program 2:
php
<?php
// Original input string
$original_string = "India To Become World's Fifth
Largest Economy In 2018.";
// Replace string
$replace_string = 5;
// This function call will not show the expected output as the
// function interpret the number to ordinal value of character.
echo ereg_replace('Fifth',$replace_string, $original_string);
$original_string = "India To Become World's Fifth
Largest Economy In 2018.";
// Replace String
$replace_string = '5';
// This function call will show
// the correct expected output
echo ereg_replace('Fifth',$replace_string, $original_string);
?>
Output:
India To Become World's Largest Economy In 2018.
India To Become World's 5 Largest Economy In 2018.
Reference: https://www.php.net/manual/en/function.ereg-replace.php
Similar Reads
PHP mb_ereg_replace() Function The mb_ereg_replace() is an inbuilt function in PHP that is used to search & replace a string using the regular expression. This function is similar to the preg_match() but works on the multibyte string. Syntax: mb_ereg_replace( $pattern, $replacement, $string, $options = null): string|false|nul
1 min read
PHP mb_eregi_replace() Function The mb_eregi_replace() is an inbuilt function in PHP that is used to perform the replacement of regular expression characters. It is a case-insensitive regular expression search and replace with multi-byte character support. Syntax: mb_eregi_replace( string $pattern, string $replacement, string $str
2 min read
PHP array_replace() Function The array_replace() function is a builtin function in PHP and it takes a list of arrays separated by commas (,) as parameters and replaces all those values of the first array that have same keys in the other arrays. The replacement is done according to the following rules: If a key in the first arra
3 min read
PHP str_replace() Function In this article, we will see how to replace the occurrence of the search string with the replacing string using the str_replace() function in PHP, along with understanding their implementation through the examples. The str_replace() is a built-in function in PHP and is used to replace all the occurr
3 min read
PHP mb_ereg_replace_callback() Function The mb_ereg_replace_callback() function is an inbuilt function in PHP that is used to replace a regular expression match in a string using the callback function. Syntax: mb_ereg_replace_callback( $pattern, $callback,$string,$options): string|false|null Parameters: The function accepts four parameter
2 min read
PHP ereg() Function The Ereg() function in PHP searches a string to match the regular expression given in the pattern. The function is case-sensitive. This function has been deprecated in PHP 5.3.0 and removed in PHP 7.0.0. Syntax: int ereg ( string $pattern , string $str, array &$arr ); Parameters: pattern: It is
2 min read