PHP – mb_ereg_replace() function – Replace regular expression with multibyte support


In PHP, mb_ereg_replace() is used to replace a regular expression with a multibyte support. It scans the string for matches to pattern, then it replaces the matched text with the replacement.

Syntax

string mb_ereg_replace(str $pattern, $str $replacement, str $string, str $options)

Parameters

The function accepts the following four parameters −

  • $pattern − This parameter is used for the regular expression pattern. It may use multibyte characters in a pattern.

  • $replacement − This replacement parameter is used to replace the given text.

  • $string − This parameter is used to check the string.

  • $options − This parameter is used to check the search option.

Return Values

mb_ereg_replace() returns success for the resultant string or it returns False on error. It returns NULL if the string is not valid for the current encoding.

Example

In this example, UTF-8 encodings are used. The mb_ereg_replace() function will replace the small "h" with a capital "H" and it will return "Hello World" instead of "hello World".

<?php
   $result=mb_regex_encoding("UTF-8");
   $string = mb_ereg_replace( "[h]","H","hello World");
   var_dump($result);

   // It returns h as a H
   echo "$string";
?>

Output

bool(true)
Hello World

Updated on: 11-Oct-2021

572 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements