如何解码 php 中的 &#39

How to decode &#39 in php

我正在使用 api returns 字符串,其中包含这样的编码:

"It's a bit of a slow week"

我希望使用 php 将其解码为人类可读的格式。

我试过 html_entity_decoderawurldecodequoted_printable_decode。我什至检查了 Whosebug 问题并尝试了更多涉及的策略,包括 this one 无济于事(无论如何它使用不推荐使用的语法,我不希望将它保留在我的应用程序中)。

那么有人知道这是什么类型的编码,以及如何在 php 中对其进行解码吗?

取自这里的评论http://php.net/html_entity_decode#104617

If you need something that converts &#[0-9]+ entities to UTF-8, this is simple and works:

<?php
$input = "Fovi&#269;";

$output = preg_replace_callback("/(&#[0-9]+;)/", function($m) { return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); }, $input);

/* Plain UTF-8. */
echo $output;
?>

看起来效果不错。

html_entity_decode() 默认情况下忽略引号,但如果您添加 ENT_QUOTES 标志,将会执行您想要的操作:

<?php
    $a = "It&#39;s working fine.";
    $b = html_entity_decode($a, ENT_QUOTES);
    var_dump($b); // string(18) "It's working fine." 
?>

Fiddle here

PHP Reference