如何解码'在PHP中

时间:2015-03-11 20:23:09

标签: php encoding character-encoding

我正在使用一个api,它返回包含如下编码的字符串:

"It's a bit of a slow week"

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

我已尝试html_entity_decoderawurldecodequoted_printable_decode。我甚至检查了stackoverflow问题并尝试了更多参与的策略,包括this one无济于事(无论如何它使用了弃用的语法,我也不喜欢将它保留在我的应用程序中)。

所以有人知道这是什么类型的编码,以及如何在php中解码它?

2 个答案:

答案 0 :(得分:16)

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

答案 1 :(得分:1)

取自http://php.net/html_entity_decode#104617

的评论
  

如果您需要将&amp;#[0-9] +实体转换为UTF-8的内容,请执行此操作   很简单,有效:

<?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;
?>

似乎运作良好。