| @@ 2446-2492 (lines=47) @@ | ||
| 2443 | * |
|
| 2444 | * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE. |
|
| 2445 | */ |
|
| 2446 | public static function is_utf16($str) |
|
| 2447 | { |
|
| 2448 | if (self::is_binary($str)) { |
|
| 2449 | self::checkForSupport(); |
|
| 2450 | ||
| 2451 | $maybeUTF16LE = 0; |
|
| 2452 | $test = mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 2453 | if ($test !== false && strlen($test) > 1) { |
|
| 2454 | $test2 = mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 2455 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 2456 | if ($test3 == $test) { |
|
| 2457 | $strChars = self::count_chars($str); |
|
| 2458 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2459 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2460 | $maybeUTF16LE++; |
|
| 2461 | } |
|
| 2462 | } |
|
| 2463 | } |
|
| 2464 | } |
|
| 2465 | ||
| 2466 | $maybeUTF16BE = 0; |
|
| 2467 | $test = mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 2468 | if ($test !== false && strlen($test) > 1) { |
|
| 2469 | $test2 = mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 2470 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 2471 | if ($test3 == $test) { |
|
| 2472 | $strChars = self::count_chars($str); |
|
| 2473 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2474 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2475 | $maybeUTF16BE++; |
|
| 2476 | } |
|
| 2477 | } |
|
| 2478 | } |
|
| 2479 | } |
|
| 2480 | ||
| 2481 | if ($maybeUTF16BE != $maybeUTF16LE) { |
|
| 2482 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 2483 | return 1; |
|
| 2484 | } else { |
|
| 2485 | return 2; |
|
| 2486 | } |
|
| 2487 | } |
|
| 2488 | ||
| 2489 | } |
|
| 2490 | ||
| 2491 | return false; |
|
| 2492 | } |
|
| 2493 | ||
| 2494 | /** |
|
| 2495 | * Returns count of characters used in a string. |
|
| @@ 2518-2564 (lines=47) @@ | ||
| 2515 | * |
|
| 2516 | * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE. |
|
| 2517 | */ |
|
| 2518 | public static function is_utf32($str) |
|
| 2519 | { |
|
| 2520 | if (self::is_binary($str)) { |
|
| 2521 | self::checkForSupport(); |
|
| 2522 | ||
| 2523 | $maybeUTF32LE = 0; |
|
| 2524 | $test = mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 2525 | if ($test !== false && strlen($test) > 1) { |
|
| 2526 | $test2 = mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 2527 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 2528 | if ($test3 == $test) { |
|
| 2529 | $strChars = self::count_chars($str); |
|
| 2530 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2531 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2532 | $maybeUTF32LE++; |
|
| 2533 | } |
|
| 2534 | } |
|
| 2535 | } |
|
| 2536 | } |
|
| 2537 | ||
| 2538 | $maybeUTF32BE = 0; |
|
| 2539 | $test = mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 2540 | if ($test !== false && strlen($test) > 1) { |
|
| 2541 | $test2 = mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 2542 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 2543 | if ($test3 == $test) { |
|
| 2544 | $strChars = self::count_chars($str); |
|
| 2545 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2546 | if (in_array($test3char, $strChars, true) === true) { |
|
| 2547 | $maybeUTF32BE++; |
|
| 2548 | } |
|
| 2549 | } |
|
| 2550 | } |
|
| 2551 | } |
|
| 2552 | ||
| 2553 | if ($maybeUTF32BE != $maybeUTF32LE) { |
|
| 2554 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 2555 | return 1; |
|
| 2556 | } else { |
|
| 2557 | return 2; |
|
| 2558 | } |
|
| 2559 | } |
|
| 2560 | ||
| 2561 | } |
|
| 2562 | ||
| 2563 | return false; |
|
| 2564 | } |
|
| 2565 | ||
| 2566 | /** |
|
| 2567 | * Clean-up a and show only printable UTF-8 chars at the end. |
|