Skip to content
Related Articles

Related Articles

Improve Article

PHP | ereg_replace() Function

  • Last Updated : 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:

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.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 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: http://php.net/manual/en/function.ereg-replace.php




My Personal Notes arrow_drop_up
Recommended Articles
Page :