public static function Unicode::mimeHeaderEncode

Encodes MIME/HTTP headers that contain incorrectly encoded characters.

For example, Unicode::mimeHeaderEncode('tést.txt') returns "=?UTF-8?B?dMOpc3QudHh0?=".

See http://www.rfc-editor.org/rfc/rfc2047.txt for more information.

Notes:

  • Only encode strings that contain non-ASCII characters.
  • We progressively cut-off a chunk with self::truncateBytes(). This ensures each chunk starts and ends on a character boundary.
  • Using \n as the chunk separator may cause problems on some systems and may have to be changed to \r\n or \r.

Parameters

string $string: The header to encode.

Return value

string The mime-encoded header.

2 calls to Unicode::mimeHeaderEncode()
mime_header_encode in drupal/core/includes/unicode.inc
Encodes MIME/HTTP header values that contain incorrectly encoded characters.
UnicodeTest::testMimeHeader in drupal/core/tests/Drupal/Tests/Component/Utility/UnicodeTest.php
Tests Unicode::mimeHeaderEncode() and Unicode::mimeHeaderDecode().

File

drupal/core/lib/Drupal/Component/Utility/Unicode.php, line 525
Contains \Drupal\Component\Utility\Unicode.

Class

Unicode
Provides Unicode-related conversions and operations.

Namespace

Drupal\Component\Utility

Code

public static function mimeHeaderEncode($string) {
  if (preg_match('/[^\\x20-\\x7E]/', $string)) {
    $chunk_size = 47;

    // floor((75 - strlen("=?UTF-8?B??=")) * 0.75);
    $len = strlen($string);
    $output = '';
    while ($len > 0) {
      $chunk = static::truncateBytes($string, $chunk_size);
      $output .= ' =?UTF-8?B?' . base64_encode($chunk) . "?=\n";
      $c = strlen($chunk);
      $string = substr($string, $c);
      $len -= $c;
    }
    return trim($output);
  }
  return $string;
}