PHP土耳其语字符到ASCII给出相同的输出



ord(Ö’(给出195,ord(Ç’(也给出195。我不知道错误是什么。你们能帮我吗?

ord--将字符串的第一个字节转换为0到255 之间的值

https://www.php.net/manual/en/function.ord.php

问题是-源文件的字符集是什么?由于"Ö"one_answers"Ç"都不是ASCII符号,它们在UTF-8编码的中表示为两个字节

Ö-0xC3 0x96

Ç-0xC3 0x87

正如您所看到的,两个字符都有第一个字节0xC3(=195 dec.(

所以,你需要决定你想要得到什么代码?

例如,您可以将UTF-8字符串转换为Windows-1254:

print ord(iconv('UTF-8', 'Windows-1254', 'Ö')); // 214
print ord(iconv('UTF-8', 'Windows-1254', 'Ç')); // 199

或者您可能想要获得unicode代码点。要做到这一点,您可以首先将字符串转换为UTF-32,然后解码32位数字:

function get_codepoint($utf8char) {
$bin = iconv('UTF-8', 'UTF-32BE', $utf8char); // convert to UTF-32 big endian
$a = unpack('Ncp', $bin); // unpack binary data
return $a['cp']; // get the code point
}
print get_codepoint('Ö'); // 214
print get_codepoint('Ç'); // 199

或者在php7.2及更高版本中,您可以简单地使用mb_ord

print mb_ord('Ö'); // 214
print mb_ord('Ç'); // 199

最新更新