| @@ 2289-2335 (lines=47) @@ | ||
| 2286 | * |
|
| 2287 | * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE |
|
| 2288 | */ |
|
| 2289 | public static function is_utf16($string) |
|
| 2290 | { |
|
| 2291 | if (self::is_binary($string)) { |
|
| 2292 | self::checkForSupport(); |
|
| 2293 | ||
| 2294 | $maybeUTF16LE = 0; |
|
| 2295 | $test = mb_convert_encoding($string, 'UTF-8', 'UTF-16LE'); |
|
| 2296 | if ($test !== false && strlen($test) > 1) { |
|
| 2297 | $test2 = mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 2298 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 2299 | if ($test3 == $test) { |
|
| 2300 | $stringChars = self::count_chars($string); |
|
| 2301 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2302 | if (in_array($test3char, $stringChars, true) === true) { |
|
| 2303 | $maybeUTF16LE++; |
|
| 2304 | } |
|
| 2305 | } |
|
| 2306 | } |
|
| 2307 | } |
|
| 2308 | ||
| 2309 | $maybeUTF16BE = 0; |
|
| 2310 | $test = mb_convert_encoding($string, 'UTF-8', 'UTF-16BE'); |
|
| 2311 | if ($test !== false && strlen($test) > 1) { |
|
| 2312 | $test2 = mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 2313 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 2314 | if ($test3 == $test) { |
|
| 2315 | $stringChars = self::count_chars($string); |
|
| 2316 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2317 | if (in_array($test3char, $stringChars, true) === true) { |
|
| 2318 | $maybeUTF16BE++; |
|
| 2319 | } |
|
| 2320 | } |
|
| 2321 | } |
|
| 2322 | } |
|
| 2323 | ||
| 2324 | if ($maybeUTF16BE != $maybeUTF16LE) { |
|
| 2325 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 2326 | return 1; |
|
| 2327 | } else { |
|
| 2328 | return 2; |
|
| 2329 | } |
|
| 2330 | } |
|
| 2331 | ||
| 2332 | } |
|
| 2333 | ||
| 2334 | return false; |
|
| 2335 | } |
|
| 2336 | ||
| 2337 | /** |
|
| 2338 | * returns count of characters used in a string |
|
| @@ 2361-2407 (lines=47) @@ | ||
| 2358 | * |
|
| 2359 | * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE |
|
| 2360 | */ |
|
| 2361 | public static function is_utf32($string) |
|
| 2362 | { |
|
| 2363 | if (self::is_binary($string)) { |
|
| 2364 | self::checkForSupport(); |
|
| 2365 | ||
| 2366 | $maybeUTF32LE = 0; |
|
| 2367 | $test = mb_convert_encoding($string, 'UTF-8', 'UTF-32LE'); |
|
| 2368 | if ($test !== false && strlen($test) > 1) { |
|
| 2369 | $test2 = mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 2370 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 2371 | if ($test3 == $test) { |
|
| 2372 | $stringChars = self::count_chars($string); |
|
| 2373 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2374 | if (in_array($test3char, $stringChars, true) === true) { |
|
| 2375 | $maybeUTF32LE++; |
|
| 2376 | } |
|
| 2377 | } |
|
| 2378 | } |
|
| 2379 | } |
|
| 2380 | ||
| 2381 | $maybeUTF32BE = 0; |
|
| 2382 | $test = mb_convert_encoding($string, 'UTF-8', 'UTF-32BE'); |
|
| 2383 | if ($test !== false && strlen($test) > 1) { |
|
| 2384 | $test2 = mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 2385 | $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 2386 | if ($test3 == $test) { |
|
| 2387 | $stringChars = self::count_chars($string); |
|
| 2388 | foreach (self::count_chars($test3) as $test3char => $test3charEmpty) { |
|
| 2389 | if (in_array($test3char, $stringChars, true) === true) { |
|
| 2390 | $maybeUTF32BE++; |
|
| 2391 | } |
|
| 2392 | } |
|
| 2393 | } |
|
| 2394 | } |
|
| 2395 | ||
| 2396 | if ($maybeUTF32BE != $maybeUTF32LE) { |
|
| 2397 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 2398 | return 1; |
|
| 2399 | } else { |
|
| 2400 | return 2; |
|
| 2401 | } |
|
| 2402 | } |
|
| 2403 | ||
| 2404 | } |
|
| 2405 | ||
| 2406 | return false; |
|
| 2407 | } |
|
| 2408 | ||
| 2409 | /** |
|
| 2410 | * clean-up a UTF-8 string and show only printable chars at the end |
|