Thursday, September 4, 2025
HomeLanguagesPHP mb_regex_encoding() Function

PHP mb_regex_encoding() Function

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

RELATED ARTICLES

Most Popular

Dominic
32261 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11795 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11855 POSTS0 COMMENTS
Shaida Kate Naidoo
6747 POSTS0 COMMENTS
Ted Musemwa
7023 POSTS0 COMMENTS
Thapelo Manthata
6695 POSTS0 COMMENTS
Umr Jansen
6714 POSTS0 COMMENTS