| @@ 3003-3050 (lines=48) @@ | ||
| 3000 | * <strong>2</strong> for UTF-16BE. |
|
| 3001 | * </p> |
|
| 3002 | */ |
|
| 3003 | public static function is_utf16($str) |
|
| 3004 | { |
|
| 3005 | $str = self::remove_bom($str); |
|
| 3006 | ||
| 3007 | if (self::is_binary($str) === true) { |
|
| 3008 | ||
| 3009 | $maybeUTF16LE = 0; |
|
| 3010 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16LE'); |
|
| 3011 | if ($test) { |
|
| 3012 | $test2 = \mb_convert_encoding($test, 'UTF-16LE', 'UTF-8'); |
|
| 3013 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16LE'); |
|
| 3014 | if ($test3 === $test) { |
|
| 3015 | $strChars = self::count_chars($str, true); |
|
| 3016 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 3017 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3018 | $maybeUTF16LE++; |
|
| 3019 | } |
|
| 3020 | } |
|
| 3021 | } |
|
| 3022 | } |
|
| 3023 | ||
| 3024 | $maybeUTF16BE = 0; |
|
| 3025 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-16BE'); |
|
| 3026 | if ($test) { |
|
| 3027 | $test2 = \mb_convert_encoding($test, 'UTF-16BE', 'UTF-8'); |
|
| 3028 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-16BE'); |
|
| 3029 | if ($test3 === $test) { |
|
| 3030 | $strChars = self::count_chars($str, true); |
|
| 3031 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 3032 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3033 | $maybeUTF16BE++; |
|
| 3034 | } |
|
| 3035 | } |
|
| 3036 | } |
|
| 3037 | } |
|
| 3038 | ||
| 3039 | if ($maybeUTF16BE !== $maybeUTF16LE) { |
|
| 3040 | if ($maybeUTF16LE > $maybeUTF16BE) { |
|
| 3041 | return 1; |
|
| 3042 | } |
|
| 3043 | ||
| 3044 | return 2; |
|
| 3045 | } |
|
| 3046 | ||
| 3047 | } |
|
| 3048 | ||
| 3049 | return false; |
|
| 3050 | } |
|
| 3051 | ||
| 3052 | /** |
|
| 3053 | * Check if the string is UTF-32. |
|
| @@ 3063-3110 (lines=48) @@ | ||
| 3060 | * <strong>2</strong> for UTF-32BE. |
|
| 3061 | * </p> |
|
| 3062 | */ |
|
| 3063 | public static function is_utf32($str) |
|
| 3064 | { |
|
| 3065 | $str = self::remove_bom($str); |
|
| 3066 | ||
| 3067 | if (self::is_binary($str) === true) { |
|
| 3068 | ||
| 3069 | $maybeUTF32LE = 0; |
|
| 3070 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32LE'); |
|
| 3071 | if ($test) { |
|
| 3072 | $test2 = \mb_convert_encoding($test, 'UTF-32LE', 'UTF-8'); |
|
| 3073 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32LE'); |
|
| 3074 | if ($test3 === $test) { |
|
| 3075 | $strChars = self::count_chars($str, true); |
|
| 3076 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 3077 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3078 | $maybeUTF32LE++; |
|
| 3079 | } |
|
| 3080 | } |
|
| 3081 | } |
|
| 3082 | } |
|
| 3083 | ||
| 3084 | $maybeUTF32BE = 0; |
|
| 3085 | $test = \mb_convert_encoding($str, 'UTF-8', 'UTF-32BE'); |
|
| 3086 | if ($test) { |
|
| 3087 | $test2 = \mb_convert_encoding($test, 'UTF-32BE', 'UTF-8'); |
|
| 3088 | $test3 = \mb_convert_encoding($test2, 'UTF-8', 'UTF-32BE'); |
|
| 3089 | if ($test3 === $test) { |
|
| 3090 | $strChars = self::count_chars($str, true); |
|
| 3091 | foreach (self::count_chars($test3, true) as $test3char => $test3charEmpty) { |
|
| 3092 | if (in_array($test3char, $strChars, true) === true) { |
|
| 3093 | $maybeUTF32BE++; |
|
| 3094 | } |
|
| 3095 | } |
|
| 3096 | } |
|
| 3097 | } |
|
| 3098 | ||
| 3099 | if ($maybeUTF32BE !== $maybeUTF32LE) { |
|
| 3100 | if ($maybeUTF32LE > $maybeUTF32BE) { |
|
| 3101 | return 1; |
|
| 3102 | } |
|
| 3103 | ||
| 3104 | return 2; |
|
| 3105 | } |
|
| 3106 | ||
| 3107 | } |
|
| 3108 | ||
| 3109 | return false; |
|
| 3110 | } |
|
| 3111 | ||
| 3112 | /** |
|
| 3113 | * Checks whether the passed string contains only byte sequences that appear valid UTF-8 characters. |
|