3v4l.org

run code in 300+ PHP versions simultaneously
<?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) { var_dump($old, $current); 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";
Output for 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
string(5) "UTF-8" string(6) "UTF-16" bool(true) 3134 - UTF-16 3134 - UTF-16 4451 - UTF-16LE 4451 - UTF-16LE
Output for 8.0.1 - 8.0.30
string(5) "UTF-8" string(1) "1" bool(true) 3134 - UTF-8 3134 - UTF-8 4451 - UTF-8 4451 - UTF-8

preferences:
101.09 ms | 401 KiB | 85 Q