PHP 中 ord 或 charCodeAt() 的 UTF-8 安全等价物

2021-12-28 00:00:00 utf-8 character-encoding php javascript

我需要能够使用 ord() 获得与 javascript 的 charCodeAt() 函数相同的值.问题是 ord() 不支持 UTF8.

I need to be able to use ord() to get the same value as javascript's charCodeAt() function. The problem is that ord() doesn't support UTF8.

如何在 PHP 中将 Ą 转换为 260?我尝试了一些 uniord 函数,但它们都报告 256 而不是 260.

How can I get Ą to translate to 260 in PHP? I've tried some uniord functions out there, but they all report 256 instead of 260.

非常感谢您的帮助!

问候

推荐答案

ord() 以字节为单位工作(作为大多数 PHP 标准字符串函数 - 如果不是全部).您需要自己转换它,例如在多字节字符串扩展的帮助下:

ord() works byte per byte (as most of PHPs standard string functions - if not all). You would need to convert it your own, for example with the help of the multibyte string extension:

$utf8Character = 'Ą';
list(, $ord) = unpack('N', mb_convert_encoding($utf8Character, 'UCS-4BE', 'UTF-8'));
echo $ord; # 260

相关文章