Tuesday, November 19, 2024
Google search engine
HomeData Modelling & AIPHP | ereg_replace() Function

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. 

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 = "Geeksforneveropen PHP article."; 
       $string_pattern = "(.*)PHP(.*)"; 
       $replace_string = " You should read \\1all\\2"; 
Output: You should read Geeksforneveropen all article.
Explanation: Within the parenthesis "\1" and "\2" to access
             the part of string and replace with 'PHP' to 'all'.

Input: $original_string = "Geeksforneveropen is no:one computer 
                                             science portal.";
       $replace_string = '1'; 
       $original_string = ereg_replace('one', $replace_string,
                                             $original_string);
Output: Geeksforneveropen 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: http://php.net/manual/en/function.ereg-replace.php

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments