Code Duplication    Length = 47-47 lines in 2 locations

src/voku/helper/UTF8.php 2 locations

@@ 2832-2878 (lines=47) @@
2829
   *
2830
   * @return int|false false if is't not UTF16, 1 for UTF-16LE, 2 for UTF-16BE.
2831
   */
2832
  public static function is_utf16($str)
2833
  {
2834
    if (self::is_binary($str)) {
2835
      self::checkForSupport();
2836
2837
      $maybeUTF16LE = 0;
2838
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-16LE');
2839
      if ($test !== false && strlen($test) > 1) {
2840
        $test2 = mb_convert_encoding($test, 'UTF-16LE', 'UTF-8');
2841
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE');
2842
        if ($test3 == $test) {
2843
          $strChars = self::count_chars($str);
2844
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
2845
            if (in_array($test3char, $strChars, true) === true) {
2846
              $maybeUTF16LE++;
2847
            }
2848
          }
2849
        }
2850
      }
2851
2852
      $maybeUTF16BE = 0;
2853
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-16BE');
2854
      if ($test !== false && strlen($test) > 1) {
2855
        $test2 = mb_convert_encoding($test, 'UTF-16BE', 'UTF-8');
2856
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE');
2857
        if ($test3 == $test) {
2858
          $strChars = self::count_chars($str);
2859
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
2860
            if (in_array($test3char, $strChars, true) === true) {
2861
              $maybeUTF16BE++;
2862
            }
2863
          }
2864
        }
2865
      }
2866
2867
      if ($maybeUTF16BE != $maybeUTF16LE) {
2868
        if ($maybeUTF16LE > $maybeUTF16BE) {
2869
          return 1;
2870
        } else {
2871
          return 2;
2872
        }
2873
      }
2874
2875
    }
2876
2877
    return false;
2878
  }
2879
2880
  /**
2881
   * Check if the string is UTF-32.
@@ 2887-2933 (lines=47) @@
2884
   *
2885
   * @return int|false false if is't not UTF16, 1 for UTF-32LE, 2 for UTF-32BE.
2886
   */
2887
  public static function is_utf32($str)
2888
  {
2889
    if (self::is_binary($str)) {
2890
      self::checkForSupport();
2891
2892
      $maybeUTF32LE = 0;
2893
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-32LE');
2894
      if ($test !== false && strlen($test) > 1) {
2895
        $test2 = mb_convert_encoding($test, 'UTF-32LE', 'UTF-8');
2896
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE');
2897
        if ($test3 == $test) {
2898
          $strChars = self::count_chars($str);
2899
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
2900
            if (in_array($test3char, $strChars, true) === true) {
2901
              $maybeUTF32LE++;
2902
            }
2903
          }
2904
        }
2905
      }
2906
2907
      $maybeUTF32BE = 0;
2908
      $test = mb_convert_encoding($str, 'UTF-8', 'UTF-32BE');
2909
      if ($test !== false && strlen($test) > 1) {
2910
        $test2 = mb_convert_encoding($test, 'UTF-32BE', 'UTF-8');
2911
        $test3 = mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE');
2912
        if ($test3 == $test) {
2913
          $strChars = self::count_chars($str);
2914
          foreach (self::count_chars($test3) as $test3char => $test3charEmpty) {
2915
            if (in_array($test3char, $strChars, true) === true) {
2916
              $maybeUTF32BE++;
2917
            }
2918
          }
2919
        }
2920
      }
2921
2922
      if ($maybeUTF32BE != $maybeUTF32LE) {
2923
        if ($maybeUTF32LE > $maybeUTF32BE) {
2924
          return 1;
2925
        } else {
2926
          return 2;
2927
        }
2928
      }
2929
2930
    }
2931
2932
    return false;
2933
  }
2934
2935
  /**
2936
   * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters.