PHP | ereg_replace() Function
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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.
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.
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.
India To Become World's Largest Economy In 2018. India To Become World's 5 Largest Economy In 2018.