| @@ 2526-2573 (lines=48) @@ | ||
| 2523 | * <strong>2</strong> for UTF-16BE. |
|
| 2524 | * </p> |
|
| 2525 | */ |
|
| 2526 | public static function is_utf16($str) |
|
| 2527 | { |
|
| 2528 | $str = self::remove_bom($str); |
|
| 2529 | ||
| 2530 | if (self::is_binary($str)) { |
|
| 2531 | ||
| 2532 | $maybeUTF16LE = 0; |
|
| 2533 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 2534 | if ($test) { |
|
| 2535 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 2536 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 2537 | if ($test3 === $test) { |
|
| 2538 | $strChars = self::count_chars($str, true); |
|
| 2539 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2540 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2541 | $maybeUTF16LE++; |
|
| 2542 | } |
|
| 2543 | } |
|
| 2544 | } |
|
| 2545 | } |
|
| 2546 | ||
| 2547 | $maybeUTF16BE = 0; |
|
| 2548 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 2549 | if ($test) { |
|
| 2550 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 2551 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 2552 | if ($test3 === $test) { |
|
| 2553 | $strChars = self::count_chars($str, true); |
|
| 2554 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2555 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2556 | $maybeUTF16BE++; |
|
| 2557 | } |
|
| 2558 | } |
|
| 2559 | } |
|
| 2560 | } |
|
| 2561 | ||
| 2562 | if ($maybeUTF16BE !== $maybeUTF16LE) { |
|
| 2563 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 2564 | return 1; |
|
| 2565 | } else { |
|
| 2566 | return 2; |
|
| 2567 | } |
|
| 2568 | } |
|
| 2569 | ||
| 2570 | } |
|
| 2571 | ||
| 2572 | return false; |
|
| 2573 | } |
|
| 2574 | ||
| 2575 | /** |
|
| 2576 | * Check if the string is UTF-32. |
|
| @@ 2586-2633 (lines=48) @@ | ||
| 2583 | * <strong>2</strong> for UTF-32BE. |
|
| 2584 | * </p> |
|
| 2585 | */ |
|
| 2586 | public static function is_utf32($str) |
|
| 2587 | { |
|
| 2588 | $str = self::remove_bom($str); |
|
| 2589 | ||
| 2590 | if (self::is_binary($str)) { |
|
| 2591 | ||
| 2592 | $maybeUTF32LE = 0; |
|
| 2593 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 2594 | if ($test) { |
|
| 2595 | $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 2596 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 2597 | if ($test3 === $test) { |
|
| 2598 | $strChars = self::count_chars($str, true); |
|
| 2599 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2600 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2601 | $maybeUTF32LE++; |
|
| 2602 | } |
|
| 2603 | } |
|
| 2604 | } |
|
| 2605 | } |
|
| 2606 | ||
| 2607 | $maybeUTF32BE = 0; |
|
| 2608 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 2609 | if ($test) { |
|
| 2610 | $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 2611 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 2612 | if ($test3 === $test) { |
|
| 2613 | $strChars = self::count_chars($str, true); |
|
| 2614 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 2615 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2616 | $maybeUTF32BE++; |
|
| 2617 | } |
|
| 2618 | } |
|
| 2619 | } |
|
| 2620 | } |
|
| 2621 | ||
| 2622 | if ($maybeUTF32BE !== $maybeUTF32LE) { |
|
| 2623 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 2624 | return 1; |
|
| 2625 | } else { |
|
| 2626 | return 2; |
|
| 2627 | } |
|
| 2628 | } |
|
| 2629 | ||
| 2630 | } |
|
| 2631 | ||
| 2632 | return false; |
|
| 2633 | } |
|
| 2634 | ||
| 2635 | /** |
|
| 2636 | * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters. |
|