| @@ 2379-2425 (lines=47) @@ | ||
| 2376 | * |
|
| 2377 | * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE. |
|
| 2378 | */ |
|
| 2379 | public static function is_utf16($string) |
|
| 2380 | { |
|
| 2381 | if (self::is_binary($string)) { |
|
| 2382 | self::checkForSupport(); |
|
| 2383 | ||
| 2384 | $maybeUTF16LE = 0; |
|
| 2385 | $test = mb_convert_encoding($string, 'UTF-8', 'UTF-16LE'); |
|
| 2386 | if ($test !== false && strlen($test) > 1) { |
|
| 2387 | $test2 = mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 2388 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 2389 | if ($test3 == $test) { |
|
| 2390 | $stringChars = self::count_chars($string); |
|
| 2391 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2392 | if (in_array($test3char, $stringChars, true) === true) { |
|
| 2393 | $maybeUTF16LE++; |
|
| 2394 | } |
|
| 2395 | } |
|
| 2396 | } |
|
| 2397 | } |
|
| 2398 | ||
| 2399 | $maybeUTF16BE = 0; |
|
| 2400 | $test = mb_convert_encoding($string, 'UTF-8', 'UTF-16BE'); |
|
| 2401 | if ($test !== false && strlen($test) > 1) { |
|
| 2402 | $test2 = mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 2403 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 2404 | if ($test3 == $test) { |
|
| 2405 | $stringChars = self::count_chars($string); |
|
| 2406 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2407 | if (in_array($test3char, $stringChars, true) === true) { |
|
| 2408 | $maybeUTF16BE++; |
|
| 2409 | } |
|
| 2410 | } |
|
| 2411 | } |
|
| 2412 | } |
|
| 2413 | ||
| 2414 | if ($maybeUTF16BE != $maybeUTF16LE) { |
|
| 2415 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 2416 | return 1; |
|
| 2417 | } else { |
|
| 2418 | return 2; |
|
| 2419 | } |
|
| 2420 | } |
|
| 2421 | ||
| 2422 | } |
|
| 2423 | ||
| 2424 | return false; |
|
| 2425 | } |
|
| 2426 | ||
| 2427 | /** |
|
| 2428 | * Returns count of characters used in a string. |
|
| @@ 2451-2497 (lines=47) @@ | ||
| 2448 | * |
|
| 2449 | * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE. |
|
| 2450 | */ |
|
| 2451 | public static function is_utf32($string) |
|
| 2452 | { |
|
| 2453 | if (self::is_binary($string)) { |
|
| 2454 | self::checkForSupport(); |
|
| 2455 | ||
| 2456 | $maybeUTF32LE = 0; |
|
| 2457 | $test = mb_convert_encoding($string, 'UTF-8', 'UTF-32LE'); |
|
| 2458 | if ($test !== false && strlen($test) > 1) { |
|
| 2459 | $test2 = mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 2460 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 2461 | if ($test3 == $test) { |
|
| 2462 | $stringChars = self::count_chars($string); |
|
| 2463 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2464 | if (in_array($test3char, $stringChars, true) === true) { |
|
| 2465 | $maybeUTF32LE++; |
|
| 2466 | } |
|
| 2467 | } |
|
| 2468 | } |
|
| 2469 | } |
|
| 2470 | ||
| 2471 | $maybeUTF32BE = 0; |
|
| 2472 | $test = mb_convert_encoding($string, 'UTF-8', 'UTF-32BE'); |
|
| 2473 | if ($test !== false && strlen($test) > 1) { |
|
| 2474 | $test2 = mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 2475 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 2476 | if ($test3 == $test) { |
|
| 2477 | $stringChars = self::count_chars($string); |
|
| 2478 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2479 | if (in_array($test3char, $stringChars, true) === true) { |
|
| 2480 | $maybeUTF32BE++; |
|
| 2481 | } |
|
| 2482 | } |
|
| 2483 | } |
|
| 2484 | } |
|
| 2485 | ||
| 2486 | if ($maybeUTF32BE != $maybeUTF32LE) { |
|
| 2487 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 2488 | return 1; |
|
| 2489 | } else { |
|
| 2490 | return 2; |
|
| 2491 | } |
|
| 2492 | } |
|
| 2493 | ||
| 2494 | } |
|
| 2495 | ||
| 2496 | return false; |
|
| 2497 | } |
|
| 2498 | ||
| 2499 | /** |
|
| 2500 | * Clean-up a and show only printable UTF-8 chars at the end. |
|