diff --git a/src/functions.php b/src/functions.php index bf7537a8..964f1032 100644 --- a/src/functions.php +++ b/src/functions.php @@ -22,6 +22,7 @@ use function count; use function is_array; use function iterator_to_array; + use function rsort; use function strpos; use const COUNT_RECURSIVE; @@ -33,8 +34,11 @@ function bom_match(string $str): string { static $list; + if (null === $list) { + $list = (new ReflectionClass(ByteSequence::class))->getConstants(); - $list = $list ?? (new ReflectionClass(ByteSequence::class))->getConstants(); + rsort($list); + } foreach ($list as $sequence) { if (0 === strpos($str, $sequence)) { diff --git a/tests/ByteSequenceTest.php b/tests/ByteSequenceTest.php index 31465e08..e79292be 100644 --- a/tests/ByteSequenceTest.php +++ b/tests/ByteSequenceTest.php @@ -55,6 +55,10 @@ public function ByteSequenceMatchProvider(): array 'sequence' => 'The quick brown fox '.ByteSequence::BOM_UTF8.' jumps over the lazy dog', 'expected' => '', ], + 'UTF32 LE BOM sequence' => [ + 'sequence' => chr(255).chr(254).chr(0).chr(0), + 'expected' => ByteSequence::BOM_UTF32_LE, + ], ]; } }