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