| @@ 3414-3460 (lines=47) @@ | ||
| 3411 | * |
|
| 3412 | * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE. |
|
| 3413 | */ |
|
| 3414 | public static function is_utf16($str) |
|
| 3415 | { |
|
| 3416 | if (self::is_binary($str)) { |
|
| 3417 | self::checkForSupport(); |
|
| 3418 | ||
| 3419 | $maybeUTF16LE = 0; |
|
| 3420 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 3421 | if ($test !== false && strlen($test) > 1) { |
|
| 3422 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 3423 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 3424 | if ($test3 === $test) { |
|
| 3425 | $strChars = self::count_chars($str); |
|
| 3426 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3427 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3428 | $maybeUTF16LE++; |
|
| 3429 | } |
|
| 3430 | } |
|
| 3431 | } |
|
| 3432 | } |
|
| 3433 | ||
| 3434 | $maybeUTF16BE = 0; |
|
| 3435 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 3436 | if ($test !== false && strlen($test) > 1) { |
|
| 3437 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 3438 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 3439 | if ($test3 === $test) { |
|
| 3440 | $strChars = self::count_chars($str); |
|
| 3441 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3442 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3443 | $maybeUTF16BE++; |
|
| 3444 | } |
|
| 3445 | } |
|
| 3446 | } |
|
| 3447 | } |
|
| 3448 | ||
| 3449 | if ($maybeUTF16BE !== $maybeUTF16LE) { |
|
| 3450 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 3451 | return 1; |
|
| 3452 | } else { |
|
| 3453 | return 2; |
|
| 3454 | } |
|
| 3455 | } |
|
| 3456 | ||
| 3457 | } |
|
| 3458 | ||
| 3459 | return false; |
|
| 3460 | } |
|
| 3461 | ||
| 3462 | /** |
|
| 3463 | * Check if the string is UTF-32. |
|
| @@ 3469-3515 (lines=47) @@ | ||
| 3466 | * |
|
| 3467 | * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE. |
|
| 3468 | */ |
|
| 3469 | public static function is_utf32($str) |
|
| 3470 | { |
|
| 3471 | if (self::is_binary($str)) { |
|
| 3472 | self::checkForSupport(); |
|
| 3473 | ||
| 3474 | $maybeUTF32LE = 0; |
|
| 3475 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 3476 | if ($test !== false && strlen($test) > 1) { |
|
| 3477 | $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 3478 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 3479 | if ($test3 === $test) { |
|
| 3480 | $strChars = self::count_chars($str); |
|
| 3481 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3482 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3483 | $maybeUTF32LE++; |
|
| 3484 | } |
|
| 3485 | } |
|
| 3486 | } |
|
| 3487 | } |
|
| 3488 | ||
| 3489 | $maybeUTF32BE = 0; |
|
| 3490 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 3491 | if ($test !== false && strlen($test) > 1) { |
|
| 3492 | $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 3493 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 3494 | if ($test3 === $test) { |
|
| 3495 | $strChars = self::count_chars($str); |
|
| 3496 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3497 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3498 | $maybeUTF32BE++; |
|
| 3499 | } |
|
| 3500 | } |
|
| 3501 | } |
|
| 3502 | } |
|
| 3503 | ||
| 3504 | if ($maybeUTF32BE !== $maybeUTF32LE) { |
|
| 3505 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 3506 | return 1; |
|
| 3507 | } else { |
|
| 3508 | return 2; |
|
| 3509 | } |
|
| 3510 | } |
|
| 3511 | ||
| 3512 | } |
|
| 3513 | ||
| 3514 | return false; |
|
| 3515 | } |
|
| 3516 | ||
| 3517 | /** |
|
| 3518 | * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters. |
|