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