Open In App

PHP mb_regex_encoding() Function

Last Updated : 07 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The mb_regex_encoding() is an inbuilt function in PHP that is used for setting and retrieving the encoding for the multibyte regular expression.

Syntax:

mb_regex_encoding(?string $encoding = null): string|bool

Parameters: The function accept only one parameter which is described below.

  • $encoding:  The parameter defines which encoding you will be going to use in this program. If the encoding will be omitted, it will use an internal encoding.

Return Values: The return type of mb_regex_encoding() is mixed, which means that the function can return different types of values depending on the context in which it is used. If you call mb_regex_encoding() without any arguments, it will return the current character encoding as a string.

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

PHP




<?php
mb_regex_encoding('ISO-8859-1');
     
   // Search a string using a regular expression 
   // with ISO-8859-1 characters
   $string = 'The café is closed.';
if (mb_ereg('^The café is closed.$', $string)) {
    echo 'The string matches the regular expression!'
} else {
    echo 'The string does not match the regular expression.';
}
?>


Output:

The string matches the regular expression! 

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

PHP




<?php
mb_regex_encoding('UTF-8');
$success = mb_regex_encoding('ISO-8859-1');
  
if ($success) {
   echo 'New encoding set successfully.';
} else {
   echo 'Failed to set new encoding.';
}
?>


Output:

New encoding set successfully. 

Reference: https://www.php.net/manual/en/function.mb-regex-encoding.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads