2

我正在为一个站点开发一个 imdb 数据抓取器,我他们似乎以一种我以前从未见过的奇怪编码对所有内容进行编码。

<a href="/keyword/exploding-ship/">Exploding&#xA0;Ship</a>
A Bug&#x27;s Life

是否有将这些转换为常规字符的 php 函数?

4

2 回答 2

5

这不是编码,它是 html 实体的十六进制代码。

尝试

$converted = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
于 2009-10-16T08:01:18.827 回答
1

这些是 SGML 字符转义。它们可以是十进制 ( &#39;) 或十六进制 ( &#xA0),并直接引用 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);
}
于 2009-10-16T07:53:59.950 回答