W3cubDocs

/Drupal 8

public static function Unicode::encodingFromBOM

public static Unicode::encodingFromBOM($data)

Decodes UTF byte-order mark (BOM) into the encoding's 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.

File

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

Class

Unicode
Provides Unicode-related conversions and operations.

Namespace

Drupal\Component\Utility

Code

public static function encodingFromBOM($data) {
  static $bomMap = array(
    "\xEF\xBB\xBF" => 'UTF-8',
    "\xFE\xFF" => 'UTF-16BE',
    "\xFF\xFE" => 'UTF-16LE',
    "\x00\x00\xFE\xFF" => 'UTF-32BE',
    "\xFF\xFE\x00\x00" => 'UTF-32LE',
    "\x2B\x2F\x76\x38" => 'UTF-7',
    "\x2B\x2F\x76\x39" => 'UTF-7',
    "\x2B\x2F\x76\x2B" => 'UTF-7',
    "\x2B\x2F\x76\x2F" => 'UTF-7',
    "\x2B\x2F\x76\x38\x2D" => 'UTF-7',
  );

  foreach ($bomMap as $bom => $encoding) {
    if (strpos($data, $bom) === 0) {
      return $encoding;
    }
  }
  return FALSE;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Component!Utility!Unicode.php/function/Unicode::encodingFromBOM/8.1.x