用于Windows编码的mb_check_encoding

时间:2013-12-31 03:22:16

标签: php encoding

我得到一个输入字符串并尝试检查它是否是一个有效的windows-1255字符串:

mb_check_encoding($string, 'windows-1255'); 

我收到一条错误消息:“无效编码”windows-1255“”

编码名称'windows-1255'可能是正确的,因为我在“iconv”函数中使用它并且它工作正常。我也试过“WINDOWS-1255”和“Windows-1255”并得到了相同的结果。

如何检查字符串是否为有效的windows-1255编码?

1 个答案:

答案 0 :(得分:0)

根据我的经验,尝试嗅探编码始终以某种方式打破。

以下未经过测试,但如果编码已在您的系统中注册,则 应该。在使用之前,请确保彻底测试(底片和正面)。

您可以使用iconv()将其从Windows-1255转换为UTF-8并返回。如果它仍然是相同的字符串,则它是有效的Windows-1255。

$string = "צקר"; // the source file needs to be Windows-1255 as well 
$string_1255 = iconv ("Windows-1255", "UTF-8//IGNORE", $string);
$string_final = iconv ("UTF-8", "Windows-1255//IGNORE", $string_1255);

if ($string == $string_final)  
    echo "Yay!!! :)"; 
   else 
    echo "No :(";