我正在为一个站点开发一个 imdb 数据抓取器,我他们似乎以一种我以前从未见过的奇怪编码对所有内容进行编码。
<a href="/keyword/exploding-ship/">Exploding Ship</a>
A Bug's Life
是否有将这些转换为常规字符的 php 函数?
我正在为一个站点开发一个 imdb 数据抓取器,我他们似乎以一种我以前从未见过的奇怪编码对所有内容进行编码。
<a href="/keyword/exploding-ship/">Exploding Ship</a>
A Bug's Life
是否有将这些转换为常规字符的 php 函数?
这不是编码,它是 html 实体的十六进制代码。
尝试
$converted = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
这些是 SGML 字符转义。它们可以是十进制 ( '
) 或十六进制 (  
),并直接引用 Unicode 代码点。
html_entity_decode()应该在 PHP 5 中工作。虽然我目前无法测试。
在该参考页面的第一条评论中,为旧 PHP 版本提供了以下代码:
// For users prior to PHP 4.3.0 you may do this:
function unhtmlentities($string)
{
// replace numeric entities
$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
$string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
// replace literal entities
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
$trans_tbl = array_flip($trans_tbl);
return strtr($string, $trans_tbl);
}