| @@ 3472-3518 (lines=47) @@ | ||
| 3469 | * |
|
| 3470 | * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE. |
|
| 3471 | */ |
|
| 3472 | public static function is_utf16($str) |
|
| 3473 | { |
|
| 3474 | if (self::is_binary($str)) { |
|
| 3475 | self::checkForSupport(); |
|
| 3476 | ||
| 3477 | $maybeUTF16LE = 0; |
|
| 3478 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 3479 | if ($test !== false && strlen($test) > 1) { |
|
| 3480 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 3481 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 3482 | if ($test3 === $test) { |
|
| 3483 | $strChars = self::count_chars($str); |
|
| 3484 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3485 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3486 | $maybeUTF16LE++; |
|
| 3487 | } |
|
| 3488 | } |
|
| 3489 | } |
|
| 3490 | } |
|
| 3491 | ||
| 3492 | $maybeUTF16BE = 0; |
|
| 3493 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 3494 | if ($test !== false && strlen($test) > 1) { |
|
| 3495 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 3496 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 3497 | if ($test3 === $test) { |
|
| 3498 | $strChars = self::count_chars($str); |
|
| 3499 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3500 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3501 | $maybeUTF16BE++; |
|
| 3502 | } |
|
| 3503 | } |
|
| 3504 | } |
|
| 3505 | } |
|
| 3506 | ||
| 3507 | if ($maybeUTF16BE !== $maybeUTF16LE) { |
|
| 3508 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 3509 | return 1; |
|
| 3510 | } else { |
|
| 3511 | return 2; |
|
| 3512 | } |
|
| 3513 | } |
|
| 3514 | ||
| 3515 | } |
|
| 3516 | ||
| 3517 | return false; |
|
| 3518 | } |
|
| 3519 | ||
| 3520 | /** |
|
| 3521 | * Check if the string is UTF-32. |
|
| @@ 3527-3573 (lines=47) @@ | ||
| 3524 | * |
|
| 3525 | * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE. |
|
| 3526 | */ |
|
| 3527 | public static function is_utf32($str) |
|
| 3528 | { |
|
| 3529 | if (self::is_binary($str)) { |
|
| 3530 | self::checkForSupport(); |
|
| 3531 | ||
| 3532 | $maybeUTF32LE = 0; |
|
| 3533 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 3534 | if ($test !== false && strlen($test) > 1) { |
|
| 3535 | $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 3536 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 3537 | if ($test3 === $test) { |
|
| 3538 | $strChars = self::count_chars($str); |
|
| 3539 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3540 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3541 | $maybeUTF32LE++; |
|
| 3542 | } |
|
| 3543 | } |
|
| 3544 | } |
|
| 3545 | } |
|
| 3546 | ||
| 3547 | $maybeUTF32BE = 0; |
|
| 3548 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 3549 | if ($test !== false && strlen($test) > 1) { |
|
| 3550 | $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 3551 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 3552 | if ($test3 === $test) { |
|
| 3553 | $strChars = self::count_chars($str); |
|
| 3554 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3555 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3556 | $maybeUTF32BE++; |
|
| 3557 | } |
|
| 3558 | } |
|
| 3559 | } |
|
| 3560 | } |
|
| 3561 | ||
| 3562 | if ($maybeUTF32BE !== $maybeUTF32LE) { |
|
| 3563 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 3564 | return 1; |
|
| 3565 | } else { |
|
| 3566 | return 2; |
|
| 3567 | } |
|
| 3568 | } |
|
| 3569 | ||
| 3570 | } |
|
| 3571 | ||
| 3572 | return false; |
|
| 3573 | } |
|
| 3574 | ||
| 3575 | /** |
|
| 3576 | * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters. |
|