<?php
$str1 = '14';
$str2 = 'DQ';
//$encodings1 = ['UTF-16LE', 'UTF-16', 'UTF-8', 'ASCII'];
$encodings1 = ['UTF-8', 'UTF-16', 'UTF-16LE', 'ASCII'];
$encodings2 = ['UTF-16LE', 'UTF-16', 'UTF-8'];
function is_multiencodings($list, $string) {
$current = '0';
foreach($list as $key => $val) {
$old = $current;
$current = mb_detect_encoding($string, $val, true);
if(!is_string($current)) {
if('0' != $old) {
$current = '' . $key;
} else {
$current = '0';
}
}
if($old && $current != $old) {
return true;
}
}
}
var_dump(is_multiencodings($encodings1, $str1));
echo bin2hex($str1) . ' - ' . mb_detect_encoding($str1, $encodings1, true)."\n";
echo bin2hex($str1) . ' - ' . mb_detect_encoding($str1, $encodings2, true)."\n";
echo bin2hex($str2) . ' - ' . mb_detect_encoding($str2, $encodings1, true)."\n";
echo bin2hex($str2) . ' - ' . mb_detect_encoding($str2, $encodings2, true)."\n";
preferences:
27.84 ms | 402 KiB | 5 Q