Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 2864-2910 (lines=47) @@
2861
   *
2862
   * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE.
2863
   */
2864
  public static function is_utf16($str)
2865
  {
2866
    if (self::is_binary($str)) {
2867
      self::checkForSupport();
2868
2869
      $maybeUTF16LE = 0;
2870
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
2871
      if ($test !== false && strlen($test) > 1) {
2872
        $test2 = mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
2873
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
2874
        if ($test3 == $test) {
2875
          $strChars = self::count_chars($str);
2876
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
2877
            if (in_array($test3char, $strChars, true) === true) {
2878
              $maybeUTF16LE++;
2879
            }
2880
          }
2881
        }
2882
      }
2883
2884
      $maybeUTF16BE = 0;
2885
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
2886
      if ($test !== false && strlen($test) > 1) {
2887
        $test2 = mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
2888
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
2889
        if ($test3 == $test) {
2890
          $strChars = self::count_chars($str);
2891
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
2892
            if (in_array($test3char, $strChars, true) === true) {
2893
              $maybeUTF16BE++;
2894
            }
2895
          }
2896
        }
2897
      }
2898
2899
      if ($maybeUTF16BE != $maybeUTF16LE) {
2900
        if ($maybeUTF16LE > $maybeUTF16BE) {
2901
          return 1;
2902
        } else {
2903
          return 2;
2904
        }
2905
      }
2906
2907
    }
2908
2909
    return false;
2910
  }
2911
2912
  /**
2913
   * Check if the string is UTF-32.
@@ 2919-2965 (lines=47) @@
2916
   *
2917
   * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE.
2918
   */
2919
  public static function is_utf32($str)
2920
  {
2921
    if (self::is_binary($str)) {
2922
      self::checkForSupport();
2923
2924
      $maybeUTF32LE = 0;
2925
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
2926
      if ($test !== false && strlen($test) > 1) {
2927
        $test2 = mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
2928
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
2929
        if ($test3 == $test) {
2930
          $strChars = self::count_chars($str);
2931
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
2932
            if (in_array($test3char, $strChars, true) === true) {
2933
              $maybeUTF32LE++;
2934
            }
2935
          }
2936
        }
2937
      }
2938
2939
      $maybeUTF32BE = 0;
2940
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
2941
      if ($test !== false && strlen($test) > 1) {
2942
        $test2 = mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
2943
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
2944
        if ($test3 == $test) {
2945
          $strChars = self::count_chars($str);
2946
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
2947
            if (in_array($test3char, $strChars, true) === true) {
2948
              $maybeUTF32BE++;
2949
            }
2950
          }
2951
        }
2952
      }
2953
2954
      if ($maybeUTF32BE != $maybeUTF32LE) {
2955
        if ($maybeUTF32LE > $maybeUTF32BE) {
2956
          return 1;
2957
        } else {
2958
          return 2;
2959
        }
2960
      }
2961
2962
    }
2963
2964
    return false;
2965
  }
2966
2967
  /**
2968
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.