Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 3285-3331 (lines=47) @@
3282
   *
3283
   * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE.
3284
   */
3285
  public static function is_utf16($str)
3286
  {
3287
    if (self::is_binary($str)) {
3288
      self::checkForSupport();
3289
3290
      $maybeUTF16LE = 0;
3291
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
3292
      if ($test !== false && strlen($test) > 1) {
3293
        $test2 = mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
3294
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
3295
        if ($test3 == $test) {
3296
          $strChars = self::count_chars($str);
3297
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3298
            if (in_array($test3char, $strChars, true) === true) {
3299
              $maybeUTF16LE++;
3300
            }
3301
          }
3302
        }
3303
      }
3304
3305
      $maybeUTF16BE = 0;
3306
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
3307
      if ($test !== false && strlen($test) > 1) {
3308
        $test2 = mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
3309
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
3310
        if ($test3 == $test) {
3311
          $strChars = self::count_chars($str);
3312
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3313
            if (in_array($test3char, $strChars, true) === true) {
3314
              $maybeUTF16BE++;
3315
            }
3316
          }
3317
        }
3318
      }
3319
3320
      if ($maybeUTF16BE != $maybeUTF16LE) {
3321
        if ($maybeUTF16LE > $maybeUTF16BE) {
3322
          return 1;
3323
        } else {
3324
          return 2;
3325
        }
3326
      }
3327
3328
    }
3329
3330
    return false;
3331
  }
3332
3333
  /**
3334
   * Check if the string is UTF-32.
@@ 3340-3386 (lines=47) @@
3337
   *
3338
   * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE.
3339
   */
3340
  public static function is_utf32($str)
3341
  {
3342
    if (self::is_binary($str)) {
3343
      self::checkForSupport();
3344
3345
      $maybeUTF32LE = 0;
3346
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
3347
      if ($test !== false && strlen($test) > 1) {
3348
        $test2 = mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
3349
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
3350
        if ($test3 == $test) {
3351
          $strChars = self::count_chars($str);
3352
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3353
            if (in_array($test3char, $strChars, true) === true) {
3354
              $maybeUTF32LE++;
3355
            }
3356
          }
3357
        }
3358
      }
3359
3360
      $maybeUTF32BE = 0;
3361
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
3362
      if ($test !== false && strlen($test) > 1) {
3363
        $test2 = mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
3364
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
3365
        if ($test3 == $test) {
3366
          $strChars = self::count_chars($str);
3367
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
3368
            if (in_array($test3char, $strChars, true) === true) {
3369
              $maybeUTF32BE++;
3370
            }
3371
          }
3372
        }
3373
      }
3374
3375
      if ($maybeUTF32BE != $maybeUTF32LE) {
3376
        if ($maybeUTF32LE > $maybeUTF32BE) {
3377
          return 1;
3378
        } else {
3379
          return 2;
3380
        }
3381
      }
3382
3383
    }
3384
3385
    return false;
3386
  }
3387
3388
  /**
3389
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.