Open In App

PHP imap_binary() Function

Last Updated : 22 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The imap_binary() function is an inbuilt function in PHP that is used to convert the 8-bit string into the base64 encoding. This function is used by some IMAP servers to represent mailbox names that contain non-ASCII characters or certain ASCII characters that are special in IMAP.

Syntax:

imap_binary(string $string) : string | false

Parameters: This function accepts only one parameter which is described below.

  • $string: This is the string parameter that contains the 8-bit string.

Return Values: The imap_binary() function if successfully executed will return a base64 encoded string otherwise this function will return “false”.

Note: Before using this function check if this is available in your environment or not. If not, then type this command

 apt-get install php-imap

References:

 

Program 1: The following program demonstrates the imap_binary() function.

PHP




<?php
  
$string = "geeksforgeeks";
$eightBitData = imap_binary($string);
  
// Output the result
echo "Original data: $string\n";
echo "Converted base64 data: $eightBitData\n";
  
?>


Output:

Original data: geeksforgeeks
Converted base64 data: Z2Vla3Nmb3JnZWVrcw==

Program 2: The following program demonstrates the imap_binary() function.

PHP




<?php
  
$utf7data1 = "&ZeVnLIqe-";
$utf7data2 = "&BfAEEAbABlAG4ALwA-";
  
// Check if the imap_binary() function exists
if (function_exists("imap_binary")) {
    $eightBitData1 = imap_binary($utf7data1);
    $eightBitData2 = imap_binary($utf7data2);
  
    // Output the results
    echo "Original UTF-7 data 1: $utf7data1\n";
    echo "Converted 8-bit data 1: $eightBitData1\n\n";
  
    echo "Original UTF-7 data 2: $utf7data2\n";
    echo "Converted 8-bit data 2: $eightBitData2\n";
} else {
    echo "imap_binary() function is not available"
        . " in this environment.\n";
}
  
?>


Output:

Original UTF-7 data 1: &ZeVnLIqe-
Converted 8-bit data 1: JlplVm5MSXFlLQ==
Original UTF-7 data 2: &BfAEEAbABlAG4ALwA-
Converted 8-bit data 2: JkJmQUVFQWJBQmxBRzRBTHdBLQ==

Reference: https://www.php.net/manual/en/function.imap-binary.php



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads