Open In App

PHP mb_ereg_replace() Function

Last Updated : 11 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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|null

Parameters: The following function accepts four parameters that are described below.

  • $pattern: This parameter used regular expression. It must be a valid regular expression.
  • $replacement: The string that is replaced according to the pattern.
  • $string:  This is the string where we search our pattern.
  • $option:  This is an optional parameter that is used for the matching option. Example ‘i’ for case insensitive and ‘m’ for multiline characters or ‘s’ for matching across lines.

Return Values: This function returns the resulting string if the function successfully executes otherwise it will return “false” on error.

Example 1: The following program demonstrates the mb_ereg_replace() function.

PHP




<?php
$pattern = "[g]";
$replace = "G";
$return = mb_ereg_replace($pattern, $replace, "geeksforgeeks");
var_export($return);
?>


Output:

'GeeksforGeeks'   

Example 2: The following program demonstrates the mb_ereg_replace() function.

PHP




<?php
$pattern = "[geeks]";
$replace = "Geeks";
$return = mb_ereg_replace($pattern, $replace, "geeksforgeeks");
var_export($return);   
?>


Output:

'GeeksGeeksGeeksGeeksGeeksforGeeksGeeksGeeksGeeksGeeks' 

Reference: https://www.php.net/manual/en/function.mb-ereg-replace.php


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads