Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Iconv.php

function Iconv::iconv_mime_encode

File

vendor/symfony/polyfill-iconv/Iconv.php, line 355

Class

Iconv
iconv implementation in pure PHP, UTF-8 centric.

Namespace

Symfony\Polyfill\Iconv

Code

public static function iconv_mime_encode($fieldName, $fieldValue, $pref = null) {
    if (!\is_array($pref)) {
        $pref = [];
    }
    $pref += [
        'scheme' => 'B',
        'input-charset' => self::$internalEncoding,
        'output-charset' => self::$internalEncoding,
        'line-length' => 76,
        'line-break-chars' => "\r\n",
    ];
    if (preg_match('/[\\x80-\\xFF]/', $fieldName)) {
        $fieldName = '';
    }
    $scheme = strtoupper(substr($pref['scheme'], 0, 1));
    $in = strtolower($pref['input-charset']);
    $out = strtolower($pref['output-charset']);
    if ('utf-8' !== $in && false === ($fieldValue = self::iconv($in, 'utf-8', $fieldValue))) {
        return false;
    }
    preg_match_all('/./us', $fieldValue, $chars);
    $chars = $chars[0] ?? [];
    $lineBreak = (int) $pref['line-length'];
    $lineStart = "=?{$pref['output-charset']}?{$scheme}?";
    $lineLength = \strlen($fieldName) + 2 + \strlen($lineStart) + 2;
    $lineOffset = \strlen($lineStart) + 3;
    $lineData = '';
    $fieldValue = [];
    $Q = 'Q' === $scheme;
    foreach ($chars as $c) {
        if ('utf-8' !== $out && false === ($c = self::iconv('utf-8', $out, $c))) {
            return false;
        }
        $o = $Q ? $c = preg_replace_callback('/[=_\\?\\x00-\\x1F\\x80-\\xFF]/', [
            __CLASS__,
            'qpByteCallback',
        ], $c) : base64_encode($lineData . $c);
        if (isset($o[$lineBreak - $lineLength])) {
            if (!$Q) {
                $lineData = base64_encode($lineData);
            }
            $fieldValue[] = $lineStart . $lineData . '?=';
            $lineLength = $lineOffset;
            $lineData = '';
        }
        $lineData .= $c;
        $Q && ($lineLength += \strlen($c));
    }
    if ('' !== $lineData) {
        if (!$Q) {
            $lineData = base64_encode($lineData);
        }
        $fieldValue[] = $lineStart . $lineData . '?=';
    }
    return $fieldName . ': ' . implode($pref['line-break-chars'] . ' ', $fieldValue);
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal