function Unicode::encodingFromBOM

Same name in other branches
  1. 8.9.x core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::encodingFromBOM()
  2. 10 core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::encodingFromBOM()
  3. 11.x core/lib/Drupal/Component/Utility/Unicode.php \Drupal\Component\Utility\Unicode::encodingFromBOM()

Decodes UTF byte-order mark (BOM) to the encoding name.

Parameters

string $data: The data possibly containing a BOM. This can be the entire contents of a file, or just a fragment containing at least the first five bytes.

Return value

string|bool The name of the encoding, or FALSE if no byte order mark was present.

2 calls to Unicode::encodingFromBOM()
CssOptimizer::loadFile in core/lib/Drupal/Core/Asset/CssOptimizer.php
Loads the stylesheet and resolves all @import commands.
JsOptimizer::optimize in core/lib/Drupal/Core/Asset/JsOptimizer.php
Optimizes an asset.

File

core/lib/Drupal/Component/Utility/Unicode.php, line 154

Class

Unicode
Provides Unicode-related conversions and operations.

Namespace

Drupal\Component\Utility

Code

public static function encodingFromBOM($data) {
    static $bomMap = [
        "" => 'UTF-8',
        "\xfe\xff" => 'UTF-16BE',
        "\xff\xfe" => 'UTF-16LE',
        "\x00\x00\xfe\xff" => 'UTF-32BE',
        "\xff\xfe\x00\x00" => 'UTF-32LE',
        "+/v8" => 'UTF-7',
        "+/v9" => 'UTF-7',
        "+/v+" => 'UTF-7',
        "+/v/" => 'UTF-7',
        "+/v8-" => 'UTF-7',
    ];
    foreach ($bomMap as $bom => $encoding) {
        if (strpos($data, $bom) === 0) {
            return $encoding;
        }
    }
    return FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.