| @@ 3536-3584 (lines=49) @@ | ||
| 3533 | * |
|
| 3534 | * @return int|false false if is't not UTF-16, 1 for UTF-16LE, 2 for UTF-16BE. |
|
| 3535 | */ |
|
| 3536 | public static function is_utf16($str) |
|
| 3537 | { |
|
| 3538 | $str = self::remove_bom($str); |
|
| 3539 | ||
| 3540 | if (self::is_binary($str)) { |
|
| 3541 | self::checkForSupport(); |
|
| 3542 | ||
| 3543 | $maybeUTF16LE = 0; |
|
| 3544 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 3545 | if ($test) { |
|
| 3546 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 3547 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 3548 | if ($test3 === $test) { |
|
| 3549 | $strChars = self::count_chars($str, true); |
|
| 3550 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 3551 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3552 | $maybeUTF16LE++; |
|
| 3553 | } |
|
| 3554 | } |
|
| 3555 | } |
|
| 3556 | } |
|
| 3557 | ||
| 3558 | $maybeUTF16BE = 0; |
|
| 3559 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 3560 | if ($test) { |
|
| 3561 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 3562 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 3563 | if ($test3 === $test) { |
|
| 3564 | $strChars = self::count_chars($str, true); |
|
| 3565 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 3566 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3567 | $maybeUTF16BE++; |
|
| 3568 | } |
|
| 3569 | } |
|
| 3570 | } |
|
| 3571 | } |
|
| 3572 | ||
| 3573 | if ($maybeUTF16BE !== $maybeUTF16LE) { |
|
| 3574 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 3575 | return 1; |
|
| 3576 | } else { |
|
| 3577 | return 2; |
|
| 3578 | } |
|
| 3579 | } |
|
| 3580 | ||
| 3581 | } |
|
| 3582 | ||
| 3583 | return false; |
|
| 3584 | } |
|
| 3585 | ||
| 3586 | /** |
|
| 3587 | * Check if the string is UTF-32. |
|
| @@ 3593-3641 (lines=49) @@ | ||
| 3590 | * |
|
| 3591 | * @return int|false false if is't not UTF-16, 1 for UTF-32LE, 2 for UTF-32BE. |
|
| 3592 | */ |
|
| 3593 | public static function is_utf32($str) |
|
| 3594 | { |
|
| 3595 | $str = self::remove_bom($str); |
|
| 3596 | ||
| 3597 | if (self::is_binary($str)) { |
|
| 3598 | self::checkForSupport(); |
|
| 3599 | ||
| 3600 | $maybeUTF32LE = 0; |
|
| 3601 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 3602 | if ($test) { |
|
| 3603 | $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 3604 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 3605 | if ($test3 === $test) { |
|
| 3606 | $strChars = self::count_chars($str, true); |
|
| 3607 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 3608 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3609 | $maybeUTF32LE++; |
|
| 3610 | } |
|
| 3611 | } |
|
| 3612 | } |
|
| 3613 | } |
|
| 3614 | ||
| 3615 | $maybeUTF32BE = 0; |
|
| 3616 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 3617 | if ($test) { |
|
| 3618 | $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 3619 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 3620 | if ($test3 === $test) { |
|
| 3621 | $strChars = self::count_chars($str, true); |
|
| 3622 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 3623 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3624 | $maybeUTF32BE++; |
|
| 3625 | } |
|
| 3626 | } |
|
| 3627 | } |
|
| 3628 | } |
|
| 3629 | ||
| 3630 | if ($maybeUTF32BE !== $maybeUTF32LE) { |
|
| 3631 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 3632 | return 1; |
|
| 3633 | } else { |
|
| 3634 | return 2; |
|
| 3635 | } |
|
| 3636 | } |
|
| 3637 | ||
| 3638 | } |
|
| 3639 | ||
| 3640 | return false; |
|
| 3641 | } |
|
| 3642 | ||
| 3643 | /** |
|
| 3644 | * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters. |
|