| @@ 2575-2622 (lines=48) @@ | ||
| 2572 | * <strong>2</strong> for UTF-16BE. |
|
| 2573 | * </p> |
|
| 2574 | */ |
|
| 2575 | public static function is_utf16($str) |
|
| 2576 | { |
|
| 2577 | $str = self::remove_bom($str); |
|
| 2578 | ||
| 2579 | if (self::is_binary($str)) { |
|
| 2580 | ||
| 2581 | $maybeUTF16LE = 0; |
|
| 2582 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 2583 | if ($test) { |
|
| 2584 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 2585 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 2586 | if ($test3 === $test) { |
|
| 2587 | $strChars = self::count_chars($str, true); |
|
| 2588 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2589 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2590 | $maybeUTF16LE++; |
|
| 2591 | } |
|
| 2592 | } |
|
| 2593 | } |
|
| 2594 | } |
|
| 2595 | ||
| 2596 | $maybeUTF16BE = 0; |
|
| 2597 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 2598 | if ($test) { |
|
| 2599 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 2600 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 2601 | if ($test3 === $test) { |
|
| 2602 | $strChars = self::count_chars($str, true); |
|
| 2603 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2604 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2605 | $maybeUTF16BE++; |
|
| 2606 | } |
|
| 2607 | } |
|
| 2608 | } |
|
| 2609 | } |
|
| 2610 | ||
| 2611 | if ($maybeUTF16BE !== $maybeUTF16LE) { |
|
| 2612 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 2613 | return 1; |
|
| 2614 | } else { |
|
| 2615 | return 2; |
|
| 2616 | } |
|
| 2617 | } |
|
| 2618 | ||
| 2619 | } |
|
| 2620 | ||
| 2621 | return false; |
|
| 2622 | } |
|
| 2623 | ||
| 2624 | /** |
|
| 2625 | * Check if the string is UTF-32. |
|
| @@ 2635-2682 (lines=48) @@ | ||
| 2632 | * <strong>2</strong> for UTF-32BE. |
|
| 2633 | * </p> |
|
| 2634 | */ |
|
| 2635 | public static function is_utf32($str) |
|
| 2636 | { |
|
| 2637 | $str = self::remove_bom($str); |
|
| 2638 | ||
| 2639 | if (self::is_binary($str)) { |
|
| 2640 | ||
| 2641 | $maybeUTF32LE = 0; |
|
| 2642 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 2643 | if ($test) { |
|
| 2644 | $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 2645 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 2646 | if ($test3 === $test) { |
|
| 2647 | $strChars = self::count_chars($str, true); |
|
| 2648 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2649 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2650 | $maybeUTF32LE++; |
|
| 2651 | } |
|
| 2652 | } |
|
| 2653 | } |
|
| 2654 | } |
|
| 2655 | ||
| 2656 | $maybeUTF32BE = 0; |
|
| 2657 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 2658 | if ($test) { |
|
| 2659 | $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 2660 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 2661 | if ($test3 === $test) { |
|
| 2662 | $strChars = self::count_chars($str, true); |
|
| 2663 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2664 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2665 | $maybeUTF32BE++; |
|
| 2666 | } |
|
| 2667 | } |
|
| 2668 | } |
|
| 2669 | } |
|
| 2670 | ||
| 2671 | if ($maybeUTF32BE !== $maybeUTF32LE) { |
|
| 2672 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 2673 | return 1; |
|
| 2674 | } else { |
|
| 2675 | return 2; |
|
| 2676 | } |
|
| 2677 | } |
|
| 2678 | ||
| 2679 | } |
|
| 2680 | ||
| 2681 | return false; |
|
| 2682 | } |
|
| 2683 | ||
| 2684 | /** |
|
| 2685 | * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters. |
|