Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

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