UTF-8 to Unicode Code Points(UTF-8 到 Unicode 代码点)
问题描述
是否有将 UTF-8 转换为 Unicode 而将非特殊字符保留为普通字母和数字的函数?
Is there a function that will change UTF-8 to Unicode leaving non special characters as normal letters and numbers?
即德语单词tchüß"将被呈现为类似于tch20AC21AC"(请注意我正在制作 Unicode 代码).
ie the German word "tchüß" would be rendered as something like "tch20AC21AC" (please note that I am making the Unicode codes up).
我正在试验以下函数,但尽管这个函数适用于 ASCII 32-127,但对于双字节字符似乎失败了:
I am experimenting with the following function, but although this one works well with ASCII 32-127, it seems to fail for double byte chars:
function strToHex ($string)
{
$hex = '';
for ($i = 0; $i < mb_strlen ($string, "utf-8"); $i++)
{
$id = ord (mb_substr ($string, $i, 1, "utf-8"));
$hex .= ($id <= 128) ? mb_substr ($string, $i, 1, "utf-8") : "&#" . $id . ";";
}
return ($hex);
}
有什么想法吗?
编辑 2:找到解决方案:PHP ord() 函数不适用于双字节字符.改用:http://nl.php.net/manual/en/function.ord.php#78032
EDIT 2: Found solution: The PHP ord() function does not work for double byte chars. Use instead: http://nl.php.net/manual/en/function.ord.php#78032
推荐答案
可以使用 iconv 将一种字符集转换为另一种字符集:
Converting one character set to another can be done with iconv:
http://php.net/manual/en/function.iconv.php
请注意,UTF 已经是 Unicode 编码了.
Note that UTF is already an Unicode encoding.
另一种方法是简单地使用具有正确字符集的 htmlentities:
Another way is simply using htmlentities with the right character set:
http://php.net/manual/en/function.htmlentities.php
这篇关于UTF-8 到 Unicode 代码点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UTF-8 到 Unicode 代码点
基础教程推荐
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01