koel/app/Services/Util.php

37 lines
1.2 KiB
PHP
Raw Normal View History

2015-12-17 17:56:48 +00:00
<?php
namespace App\Services;
class Util
{
public function __construct()
{
2020-12-22 20:11:22 +00:00
defined('UTF8_BOM') || define('UTF8_BOM', chr(0xEF) . chr(0xBB) . chr(0xBF));
defined('UTF16_LITTLE_ENDIAN_BOM') || define('UTF16_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE));
defined('UTF16_BIG_ENDIAN_BOM') || define('UTF16_BIG_ENDIAN_BOM', chr(0xFE) . chr(0xFF));
defined('UTF32_LITTLE_ENDIAN_BOM') || define('UTF32_LITTLE_ENDIAN_BOM', chr(0xFF) . chr(0xFE) . chr(0x00) . chr(0x00)); // @phpcs-ignore-line
defined('UTF32_BIG_ENDIAN_BOM') || define('UTF32_BIG_ENDIAN_BOM', chr(0x00) . chr(0x00) . chr(0xFE) . chr(0xFF)); // @phpcs-ignore-line
2015-12-17 17:56:48 +00:00
}
public function detectUTFEncoding(?string $str): ?string
2015-12-17 17:56:48 +00:00
{
switch (substr($str, 0, 2)) {
case UTF16_BIG_ENDIAN_BOM:
return 'UTF-16BE';
2020-12-22 20:11:22 +00:00
2015-12-17 17:56:48 +00:00
case UTF16_LITTLE_ENDIAN_BOM:
return 'UTF-16LE';
}
if (substr($str, 0, 3) === UTF8_BOM) {
return 'UTF-8';
2015-12-17 17:56:48 +00:00
}
return match (substr($str, 0, 4)) {
UTF32_BIG_ENDIAN_BOM => 'UTF-32BE',
UTF32_LITTLE_ENDIAN_BOM => 'UTF-32LE',
default => null,
};
2015-12-17 17:56:48 +00:00
}
}