- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一些希伯来文网站包含字符引用,例如:נוף
如果我将文件另存为 .html 并以 UTF-8 编码查看,我只能查看这些字母。
如果我尝试将其作为常规文本文件打开,则 UTF-8 编码不会显示正确的输出。
我注意到如果我打开一个文本编辑器并用 UTF-8 编写希伯来语,在这个例子中每个字符占用两个字节而不是 4 个字节行 (ו
)
知道这是 UTF-16 还是任何其他类型的字母 UTF 表示形式吗?
如果可能,如何将其转换为普通字母?
使用最新的 PHP 版本。
最佳答案
那些是 character references通过以十进制 (&#<i>n</i>;
) 或十六进制 (&#x<i>n</i>;
) 表示法指定该字符的代码点来引用 ISO 10646 中的字符。
您可以使用 html_entity_decode
解码此类字符引用以及 entities defined for HTML 4 的实体引用,所以其他引用资料如 <
, >
, &
也将被解码:
$str = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8');
如果你只想解码数字字符引用,你可以使用这个:
function html_dereference($match) {
if (strtolower($match[1][0]) === 'x') {
$codepoint = intval(substr($match[1], 1), 16);
} else {
$codepoint = intval($match[1], 10);
}
return mb_convert_encoding(pack('N', $codepoint), 'UTF-8', 'UTF-32BE');
}
$str = preg_replace_callback('/&#(x[0-9a-f]+|[0-9]+);/i', 'html_dereference', $str);
作为YuriKolovsky和 thirtydot已在 another question 中指出,似乎浏览器供应商确实“默默地”就有关字符引用映射的某些内容达成了一致,这确实与规范不同并且没有记录。
似乎有一些字符引用通常会映射到 Latin 1 supplement但实际上映射到不同的字符上。这是由于映射宁愿从 Windows-1252 而不是 ISO 8859-1 映射字符,而 ISO 8859-1 是构建 Unicode 字符集的基础。 Jukka Korpela 写了一个extensive article on this topic .
现在这是对上述函数的扩展,可以处理这个问题:
function html_character_reference_decode($string, $encoding='UTF-8', $fixMappingBug=true) {
$deref = function($match) use ($encoding, $fixMappingBug) {
if (strtolower($match[1][0]) === "x") {
$codepoint = intval(substr($match[1], 1), 16);
} else {
$codepoint = intval($match[1], 10);
}
// @see http://www.cs.tut.fi/~jkorpela/www/windows-chars.html
if ($fixMappingBug && $codepoint >= 130 && $codepoint <= 159) {
$mapping = array(
8218, 402, 8222, 8230, 8224, 8225, 710, 8240, 352, 8249,
338, 141, 142, 143, 144, 8216, 8217, 8220, 8221, 8226,
8211, 8212, 732, 8482, 353, 8250, 339, 157, 158, 376);
$codepoint = $mapping[$codepoint-130];
}
return mb_convert_encoding(pack("N", $codepoint), $encoding, "UTF-32BE");
};
return preg_replace_callback('/&#(x[0-9a-f]+|[0-9]+);/i', $deref, $string);
}
如果anonymous functions不可用(5.3.0 引入),你也可以使用 create_function
:
$deref = create_function('$match', '
$encoding = '.var_export($encoding, true).';
$fixMappingBug = '.var_export($fixMappingBug, true).';
if (strtolower($match[1][0]) === "x") {
$codepoint = intval(substr($match[1], 1), 16);
} else {
$codepoint = intval($match[1], 10);
}
// @see http://www.cs.tut.fi/~jkorpela/www/windows-chars.html
if ($fixMappingBug && $codepoint >= 130 && $codepoint <= 159) {
$mapping = array(
8218, 402, 8222, 8230, 8224, 8225, 710, 8240, 352, 8249,
338, 141, 142, 143, 144, 8216, 8217, 8220, 8221, 8226,
8211, 8212, 732, 8482, 353, 8250, 339, 157, 158, 376);
$codepoint = $mapping[$codepoint-130];
}
return mb_convert_encoding(pack("N", $codepoint), $encoding, "UTF-32BE");
');
这是另一个试图遵守 behavior of HTML 5 的函数:
function html5_decode($string, $flags=ENT_COMPAT, $charset='UTF-8') {
$deref = function($match) use ($flags, $charset) {
if ($match[1][0] === '#') {
if (strtolower($match[1][0]) === '#') {
$codepoint = intval(substr($match[1], 2), 16);
} else {
$codepoint = intval(substr($match[1], 1), 10);
}
// HTML 5 specific behavior
// @see http://dev.w3.org/html5/spec/tokenization.html#tokenizing-character-references
// handle Windows-1252 mismapping
// @see http://www.cs.tut.fi/~jkorpela/www/windows-chars.html
// @see http://dev.w3.org/html5/spec/tokenization.html#table-charref-overrides
$overrides = array(
0x00=>0xFFFD,0x80=>0x20AC,0x82=>0x201A,0x83=>0x0192,0x84=>0x201E,
0x85=>0x2026,0x86=>0x2020,0x87=>0x2021,0x88=>0x02C6,0x89=>0x2030,
0x8A=>0x0160,0x8B=>0x2039,0x8C=>0x0152,0x8E=>0x017D,0x91=>0x2018,
0x92=>0x2019,0x93=>0x201C,0x94=>0x201D,0x95=>0x2022,0x96=>0x2013,
0x97=>0x2014,0x98=>0x02DC,0x99=>0x2122,0x9A=>0x0161,0x9B=>0x203A,
0x9C=>0x0153,0x9E=>0x017E,0x9F=>0x0178);
if (isset($windows1252Mapping[$codepoint])) {
$codepoint = $windows1252Mapping[$codepoint];
}
if (($codepoint >= 0xD800 && $codepoint <= 0xDFFF) || $codepoint > 0x10FFFF) {
$codepoint = 0xFFFD;
}
if (($codepoint >= 0x0001 && $codepoint <= 0x0008) ||
($codepoint >= 0x000E && $codepoint <= 0x001F) ||
($codepoint >= 0x007F && $codepoint <= 0x009F) ||
($codepoint >= 0xFDD0 && $codepoint <= 0xFDEF) ||
in_array($codepoint, array(
0x000B, 0xFFFE, 0xFFFF, 0x1FFFE, 0x1FFFF, 0x2FFFE, 0x2FFFF,
0x3FFFE, 0x3FFFF, 0x4FFFE, 0x4FFFF, 0x5FFFE, 0x5FFFF, 0x6FFFE,
0x6FFFF, 0x7FFFE, 0x7FFFF, 0x8FFFE, 0x8FFFF, 0x9FFFE, 0x9FFFF,
0xAFFFE, 0xAFFFF, 0xBFFFE, 0xBFFFF, 0xCFFFE, 0xCFFFF, 0xDFFFE,
0xDFFFF, 0xEFFFE, 0xEFFFF, 0xFFFFE, 0xFFFFF, 0x10FFFE, 0x10FFFF))) {
$codepoint = 0xFFFD;
}
return mb_convert_encoding(pack("N", $codepoint), $charset, "UTF-32BE");
} else {
return html_entity_decode($match[0], $flags, $charset);
}
};
return preg_replace_callback('/&(#(?:x[0-9a-f]+|[0-9]+)|[A-Za-z0-9]+);/i', $deref, $string);
}
我还注意到在 PHP 5.4.0 中 html_entity_decode
function为 HTML 5 行为添加了另一个名为 ENT_HTML5 的标志。
关于php - 如何将 HTML 字符引用 (ף) 转换为常规 UTF-8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3565713/
UTF-8、UTF-16 和 UTF-32 之间有何区别? 据我所知,它们都将存储 Unicode,并且每个都使用不同数量的字节来表示字符。选择其中之一是否有优势? 最佳答案 当 ASCII 字符代表
好的。我知道这看起来像典型的“他为什么不直接用谷歌搜索或去 www.unicode.org 查一下?”问题,但对于这样一个简单的问题,在检查了两个来源后,我仍然无法回答。 我很确定这三种编码系统都支持
是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符 最佳答案 没有。 UTF-* 是可以对全范围 Unicode 字符进行编码的编码。 编码之间的差异在于每个字符使用多少字节。 关于u
是否存在可以用 UTF-16 编码但不能用 UTF-8 编码的字符 最佳答案 没有。 UTF-* 是可以对全范围 Unicode 字符进行编码的编码。 编码之间的差异在于每个字符使用多少字节。 关于u
UTF-16 是一种双字节字符编码。交换两个字节的地址将产生 UTF-16BE 和 UTF-16LE。 但我发现在 Ubuntu gedit 文本编辑器中存在名称 UTF-16 编码,以及 UTF-1
我想将 UTF-16 字符串转换为 UTF-8。我通过 Unicode 发现了 ICU 库。我在转换时遇到问题,因为默认设置是 UTF-16。我试过使用转换器: UErrorCode myError
UTF-16 需要 2 个字节,UTF-8 需要 1 个字节。 而USB是面向8bit的,UTF-8更自然。 UTF-8 向后兼容 ASCII,而 UTF-16 则不然。 UTF-16 需要 2 个字
我对将 unicode 字符转换为十六进制值有点困惑。 我正在使用这个网站获取字符的十六进制值。 ( https://www.branah.com/unicode-converter ) 如果我输入“
我已经用UTF-8编码创建了一个文件,但是我不了解其在磁盘上占用的大小的规则。这是我的完整研究: 首先,我创建了一个带有印地语字母“'”的文件,Windows 7上的文件大小为 8个字节。 现在带有两
如何将WideString(或其他长字符串)转换为UTF-8中的字节数组? 最佳答案 这样的功能将满足您的需求: function UTF8Bytes(const s: UTF8String): TB
我有一个奇怪的验证程序,用于验证utf-8字符串是否是有效的主机名(PHP中的Zend Framework主机名valdiator)。它允许IDN(国际化域名)。它将比较每个子域与由其十六进制字节表示
在 utf16 和 utf32 中,一个字节的零是否意味着空?就像在 utf8 中一样,还是我们需要 2 个和 4 个字节的零来相应地在 utf16 和 utf32 中创建 null? 最佳答案 在
这是基于我的观察,对于 mysql,默认字符集 utf8 有点误导,它不支持完整的 Unicode,因为它无法存储四字节 UTF-8 编码的字符。它实际上是 utf8mb4 字符集,它是完整的 Uni
我只有处理 ASCII(单字节字符)的经验,并且阅读了很多关于人们如何以不同方式处理 Unicode 的帖子,这些帖子提出了他们自己的一系列问题。 此时我对 Unicode 的了解非常有限,我读到过U
我明白 std::codecvt在 C++11 中执行 UTF-16 和 UTF-8 之间的转换,并且 std::codecvt执行 UTF-32 和 UTF-8 之间的转换。是否可以在 UTF-8
我正在编写一个 HTTP 服务器并使用 trivial-utf-8:write-utf-8-bytes 来响应请求。我听说Babel就像trivial-utf-8但效率更高,所以我想试一试。搜索了一段
我正在设计一个新的 CMS,但想要设计它来满足我 future 的所有需求,比如多语言内容,所以我认为 Unicode (UTF-8) 是最好的解决方案 但是通过一些搜索我得到了这篇文章 http:/
例如,假设我在字符串中有以下 xml: 如果我尝试将其插入到带有 Xml 列的 SQL Server 2005 数据库表中,我将收到以下错误(我使用的是 EF 4.1,但我认为这无关紧要): XM
我正在使用 Python CSV 库读取两个 CSV 文件。 一种使用 UTF-8-BOM 编码,另一种使用 UTF-8 编码。在我的实践中,我发现使用“utf-8-sig”作为编码类型可以读取这两个
假设我的数据库设置如下以使用 utf-8(mysql 中的完整 4mb 版本) mysql_query("SET CHARACTER SET utf8mb4"); mysql_query("SET N
我是一名优秀的程序员,十分优秀!