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