PHP – Get aliases of a known encoding type using mb_encoding_aliases()

In PHP, mb_encoding_aliases() is used to get the aliases of a known encoding type. This function returns all alternative names for a specified character encoding, which is useful when working with different systems that may use varying naming conventions for the same encoding.

Syntax

array mb_encoding_aliases(string $encoding)

Parameters

$encoding − The encoding type to check for aliases. This must be a valid encoding name recognized by the mbstring extension.

Return Value

Returns a numerically indexed array of encoding aliases on success, or false on failure.

Errors/Exceptions

If the encoding is not known, the function triggers an E_WARNING level error.

Example 1

Getting aliases for ASCII encoding with validation ?

<?php
   $encoding = 'ASCII';
   $known_encodings = mb_list_encodings();

   if (in_array($encoding, $known_encodings)) {
       $aliases = mb_encoding_aliases($encoding);
       print_r($aliases);
   } else {
       echo "Unknown ($encoding) encoding.
"; } ?>
Array
(
    [0] => ANSI_X3.4-1968
    [1] => iso-ir-6
    [2] => ANSI_X3.4-1986
    [3] => ISO_646.irv:1991
    [4] => US-ASCII
    [5] => ISO646-US
    [6] => us
    [7] => IBM367
    [8] => IBM-367
    [9] => cp367
    [10] => csASCII
)

Example 2

Direct usage with var_dump() for detailed output ?

<?php
   $array = mb_encoding_aliases("ASCII");
   var_dump($array);
?>
array(11) {
  [0]=>
  string(14) "ANSI_X3.4-1968"
  [1]=>
  string(8) "iso-ir-6"
  [2]=>
  string(14) "ANSI_X3.4-1986"
  [3]=>
  string(16) "ISO_646.irv:1991"
  [4]=>
  string(8) "US-ASCII"
  [5]=>
  string(9) "ISO646-US"
  [6]=>
  string(2) "us"
  [7]=>
  string(6) "IBM367"
  [8]=>
  string(7) "IBM-367"
  [9]=>
  string(5) "cp367"
  [10]=>
  string(7) "csASCII"
}

Conclusion

The mb_encoding_aliases() function is essential for handling character encoding compatibility across different systems. It helps identify alternative names for encodings, ensuring your applications can work with various encoding formats seamlessly.

Updated on: 2026-03-15T09:59:05+05:30

266 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements