PHP | ereg_replace() Function - GeeksforGeeks

13 Jun, 2018
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 fount 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. 

Below programs illustrate the ereg_replace() function.
Program 1:
<?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   // 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 outputecho 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: http://php.net/manual/en/function.ereg-replace.php
PHP-function
Pattern Searching
PHP
Web Technologies
Pattern Searching
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to validate HTML tag using Regular Expression
How to validate pin code of India using Regular Expression
Build a DFA to accept Binary strings that starts or ends with "01"
How to check Aadhaar number is valid or not using Regular Expression
How to validate time in 24-hour format using Regular Expression
How to Insert Form Data into Database using PHP ?
How to pop an alert message box using PHP ?
How to execute PHP code using command line ?
PHP in_array() Function
How to Upload Image into Database and Display it using PHP ?