PHP – mb_preferred_mime_name() function

The mb_preferred_mime_name() function in PHP is used to return the MIME charset string for a character encoding. It gets a MIME (Multipurpose Internet Mail Extensions) charset string for the specific encoding.

Syntax

string mb_preferred_mime_name(string $encoding)

Parameters

mb_preferred_mime_name() accepts only one parameter −

  • $encoding − The character encoding name to get the MIME charset string for.

Return Value

The function returns the MIME charset string for the given encoding, or false if no preferred MIME name exists for the encoding.

Example 1: Basic Usage

Let us see how to get MIME charset names for different encodings −

<?php
   // Get MIME name for EUC-JP encoding
   $mime_name = mb_preferred_mime_name("EUC-JP");
   echo "EUC-JP MIME name: " . $mime_name . "<br>";
   
   // Get MIME name for UTF-8 encoding
   $mime_name = mb_preferred_mime_name("UTF-8");
   echo "UTF-8 MIME name: " . $mime_name . "<br>";
   
   // Get MIME name for ISO-8859-1 encoding
   $mime_name = mb_preferred_mime_name("ISO-8859-1");
   echo "ISO-8859-1 MIME name: " . $mime_name . "<br>";
?>
EUC-JP MIME name: EUC-JP
UTF-8 MIME name: UTF-8
ISO-8859-1 MIME name: ISO-8859-1

Example 2: Handling Invalid Encodings

When an invalid or unsupported encoding is provided −

<?php
   // Try with invalid encoding
   $mime_name = mb_preferred_mime_name("INVALID-ENCODING");
   
   if ($mime_name === false) {
       echo "No MIME name found for the given encoding";
   } else {
       echo "MIME name: " . $mime_name;
   }
?>
No MIME name found for the given encoding

Common Use Cases

This function is commonly used when setting HTTP headers for proper character encoding declaration in web applications, email processing, and content type specifications.

Conclusion

The mb_preferred_mime_name() function is useful for converting internal encoding names to their standard MIME charset representations. It returns the appropriate MIME name or false for unsupported encodings.

Updated on: 2026-03-15T10:00:49+05:30

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements