Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 3441-3487 (lines=47) @@
3438
   *
3439
   * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE.
3440
   */
3441
  public static function is_utf16($str)
3442
  {
3443
    if (self::is_binary($str)) {
3444
      self::checkForSupport();
3445
3446
      $maybeUTF16LE = 0;
3447
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
3448
      if ($test !== false && strlen($test) > 1) {
3449
        $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
3450
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
3451
        if ($test3 === $test) {
3452
          $strChars = self::count_chars($str);
3453
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3454
            if (in_array($test3char, $strChars, true) === true) {
3455
              $maybeUTF16LE++;
3456
            }
3457
          }
3458
        }
3459
      }
3460
3461
      $maybeUTF16BE = 0;
3462
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
3463
      if ($test !== false && strlen($test) > 1) {
3464
        $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
3465
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
3466
        if ($test3 === $test) {
3467
          $strChars = self::count_chars($str);
3468
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3469
            if (in_array($test3char, $strChars, true) === true) {
3470
              $maybeUTF16BE++;
3471
            }
3472
          }
3473
        }
3474
      }
3475
3476
      if ($maybeUTF16BE !== $maybeUTF16LE) {
3477
        if ($maybeUTF16LE > $maybeUTF16BE) {
3478
          return 1;
3479
        } else {
3480
          return 2;
3481
        }
3482
      }
3483
3484
    }
3485
3486
    return false;
3487
  }
3488
3489
  /**
3490
   * Check if the string is UTF-32.
@@ 3496-3542 (lines=47) @@
3493
   *
3494
   * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE.
3495
   */
3496
  public static function is_utf32($str)
3497
  {
3498
    if (self::is_binary($str)) {
3499
      self::checkForSupport();
3500
3501
      $maybeUTF32LE = 0;
3502
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
3503
      if ($test !== false && strlen($test) > 1) {
3504
        $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
3505
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
3506
        if ($test3 === $test) {
3507
          $strChars = self::count_chars($str);
3508
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3509
            if (in_array($test3char, $strChars, true) === true) {
3510
              $maybeUTF32LE++;
3511
            }
3512
          }
3513
        }
3514
      }
3515
3516
      $maybeUTF32BE = 0;
3517
      $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
3518
      if ($test !== false && strlen($test) > 1) {
3519
        $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
3520
        $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
3521
        if ($test3 === $test) {
3522
          $strChars = self::count_chars($str);
3523
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3524
            if (in_array($test3char, $strChars, true) === true) {
3525
              $maybeUTF32BE++;
3526
            }
3527
          }
3528
        }
3529
      }
3530
3531
      if ($maybeUTF32BE !== $maybeUTF32LE) {
3532
        if ($maybeUTF32LE > $maybeUTF32BE) {
3533
          return 1;
3534
        } else {
3535
          return 2;
3536
        }
3537
      }
3538
3539
    }
3540
3541
    return false;
3542
  }
3543
3544
  /**
3545
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.