Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

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