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