| @@ 2594-2641 (lines=48) @@ | ||
| 2591 | * <strong>2</strong> for UTF-16BE. |
|
| 2592 | * </p> |
|
| 2593 | */ |
|
| 2594 | public static function is_utf16($str) |
|
| 2595 | { |
|
| 2596 | $str = self::remove_bom($str); |
|
| 2597 | ||
| 2598 | if (self::is_binary($str)) { |
|
| 2599 | ||
| 2600 | $maybeUTF16LE = 0; |
|
| 2601 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 2602 | if ($test) { |
|
| 2603 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 2604 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 2605 | if ($test3 === $test) { |
|
| 2606 | $strChars = self::count_chars($str, true); |
|
| 2607 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2608 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2609 | $maybeUTF16LE++; |
|
| 2610 | } |
|
| 2611 | } |
|
| 2612 | } |
|
| 2613 | } |
|
| 2614 | ||
| 2615 | $maybeUTF16BE = 0; |
|
| 2616 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 2617 | if ($test) { |
|
| 2618 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 2619 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 2620 | if ($test3 === $test) { |
|
| 2621 | $strChars = self::count_chars($str, true); |
|
| 2622 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2623 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2624 | $maybeUTF16BE++; |
|
| 2625 | } |
|
| 2626 | } |
|
| 2627 | } |
|
| 2628 | } |
|
| 2629 | ||
| 2630 | if ($maybeUTF16BE !== $maybeUTF16LE) { |
|
| 2631 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 2632 | return 1; |
|
| 2633 | } else { |
|
| 2634 | return 2; |
|
| 2635 | } |
|
| 2636 | } |
|
| 2637 | ||
| 2638 | } |
|
| 2639 | ||
| 2640 | return false; |
|
| 2641 | } |
|
| 2642 | ||
| 2643 | /** |
|
| 2644 | * Check if the string is UTF-32. |
|
| @@ 2654-2701 (lines=48) @@ | ||
| 2651 | * <strong>2</strong> for UTF-32BE. |
|
| 2652 | * </p> |
|
| 2653 | */ |
|
| 2654 | public static function is_utf32($str) |
|
| 2655 | { |
|
| 2656 | $str = self::remove_bom($str); |
|
| 2657 | ||
| 2658 | if (self::is_binary($str)) { |
|
| 2659 | ||
| 2660 | $maybeUTF32LE = 0; |
|
| 2661 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 2662 | if ($test) { |
|
| 2663 | $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 2664 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 2665 | if ($test3 === $test) { |
|
| 2666 | $strChars = self::count_chars($str, true); |
|
| 2667 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2668 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2669 | $maybeUTF32LE++; |
|
| 2670 | } |
|
| 2671 | } |
|
| 2672 | } |
|
| 2673 | } |
|
| 2674 | ||
| 2675 | $maybeUTF32BE = 0; |
|
| 2676 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 2677 | if ($test) { |
|
| 2678 | $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 2679 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 2680 | if ($test3 === $test) { |
|
| 2681 | $strChars = self::count_chars($str, true); |
|
| 2682 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2683 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2684 | $maybeUTF32BE++; |
|
| 2685 | } |
|
| 2686 | } |
|
| 2687 | } |
|
| 2688 | } |
|
| 2689 | ||
| 2690 | if ($maybeUTF32BE !== $maybeUTF32LE) { |
|
| 2691 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 2692 | return 1; |
|
| 2693 | } else { |
|
| 2694 | return 2; |
|
| 2695 | } |
|
| 2696 | } |
|
| 2697 | ||
| 2698 | } |
|
| 2699 | ||
| 2700 | return false; |
|
| 2701 | } |
|
| 2702 | ||
| 2703 | /** |
|
| 2704 | * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters. |
|