| @@ 3351-3397 (lines=47) @@ | ||
| 3348 | * |
|
| 3349 | * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE. |
|
| 3350 | */ |
|
| 3351 | public static function is_utf16($str) |
|
| 3352 | { |
|
| 3353 | if (self::is_binary($str)) { |
|
| 3354 | self::checkForSupport(); |
|
| 3355 | ||
| 3356 | $maybeUTF16LE = 0; |
|
| 3357 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 3358 | if ($test !== false && strlen($test) > 1) { |
|
| 3359 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 3360 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 3361 | if ($test3 === $test) { |
|
| 3362 | $strChars = self::count_chars($str); |
|
| 3363 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3364 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3365 | $maybeUTF16LE++; |
|
| 3366 | } |
|
| 3367 | } |
|
| 3368 | } |
|
| 3369 | } |
|
| 3370 | ||
| 3371 | $maybeUTF16BE = 0; |
|
| 3372 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 3373 | if ($test !== false && strlen($test) > 1) { |
|
| 3374 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 3375 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 3376 | if ($test3 === $test) { |
|
| 3377 | $strChars = self::count_chars($str); |
|
| 3378 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3379 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3380 | $maybeUTF16BE++; |
|
| 3381 | } |
|
| 3382 | } |
|
| 3383 | } |
|
| 3384 | } |
|
| 3385 | ||
| 3386 | if ($maybeUTF16BE !== $maybeUTF16LE) { |
|
| 3387 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 3388 | return 1; |
|
| 3389 | } else { |
|
| 3390 | return 2; |
|
| 3391 | } |
|
| 3392 | } |
|
| 3393 | ||
| 3394 | } |
|
| 3395 | ||
| 3396 | return false; |
|
| 3397 | } |
|
| 3398 | ||
| 3399 | /** |
|
| 3400 | * Check if the string is UTF-32. |
|
| @@ 3406-3452 (lines=47) @@ | ||
| 3403 | * |
|
| 3404 | * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE. |
|
| 3405 | */ |
|
| 3406 | public static function is_utf32($str) |
|
| 3407 | { |
|
| 3408 | if (self::is_binary($str)) { |
|
| 3409 | self::checkForSupport(); |
|
| 3410 | ||
| 3411 | $maybeUTF32LE = 0; |
|
| 3412 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 3413 | if ($test !== false && strlen($test) > 1) { |
|
| 3414 | $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 3415 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 3416 | if ($test3 === $test) { |
|
| 3417 | $strChars = self::count_chars($str); |
|
| 3418 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3419 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3420 | $maybeUTF32LE++; |
|
| 3421 | } |
|
| 3422 | } |
|
| 3423 | } |
|
| 3424 | } |
|
| 3425 | ||
| 3426 | $maybeUTF32BE = 0; |
|
| 3427 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 3428 | if ($test !== false && strlen($test) > 1) { |
|
| 3429 | $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 3430 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 3431 | if ($test3 === $test) { |
|
| 3432 | $strChars = self::count_chars($str); |
|
| 3433 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 3434 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3435 | $maybeUTF32BE++; |
|
| 3436 | } |
|
| 3437 | } |
|
| 3438 | } |
|
| 3439 | } |
|
| 3440 | ||
| 3441 | if ($maybeUTF32BE !== $maybeUTF32LE) { |
|
| 3442 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 3443 | return 1; |
|
| 3444 | } else { |
|
| 3445 | return 2; |
|
| 3446 | } |
|
| 3447 | } |
|
| 3448 | ||
| 3449 | } |
|
| 3450 | ||
| 3451 | return false; |
|
| 3452 | } |
|
| 3453 | ||
| 3454 | /** |
|
| 3455 | * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters. |
|