2013-04-17 37 views
2

我的数据库在带有特殊字符的JSON后面返回这个。我需要将其转换回原始字符:将编码的字符转换回原始字符 - PHP

{ "event_id":"5153", 
     "name":"Event Test", 
     "description":"Persönlichkeit Universität"", 
     "start_time":"2013-04-24 9:00 AM EST", 
     "end_time":"2013-04-24 5:00 PM EST" 
} 

我想删除所有的HTML字符。并将所有字符如&ouml转换为原始字符。所以description在上面的JSON实际上应该是这样的

PersönlichkeitUniversität大学”

我的阵列做array_walk编码阵列JSON之前,和strip_tags每个元素。 这是好的。(这导致上述JSON)

要获得字符回来,我想:

1. encoding again with utf8_encode 
    2. htmlspecialchars_decode 
    3. html_entity_decode //This one is eliminating the character altogether. 

但没有给出原始字符。

任何想法?

更新:

我尝试这样做。但现在描述字段返回为空

array_walk_recursive($results, function (&$val) { 
    $val = strip_tags(html_entity_decode($val)); 
}); 

$results = json_encode($results); 
+0

怎么样在使用html_entity_decode之前json_encoding它呢? – bwoebi

+0

在json_encode之前和之后,我看到了相同的结果。所以我不知道如何在json_encode帮助之前调用html_entity_decode? –

回答

1

html_entity_decode应该做的伎俩。我认为你的问题在别的地方。请记住json_encode只接受UTF-8字符。所以你可能需要utf8_encode你的字符串。

+0

我更新了我尝试的结果以及结果。 –

+0

请尝试在调用strip_tags的过程中添加对utf8_encode的调用。 –

+0

我试过了。没用。它显示:'Pers ö nlichkeit Universit ä t“' –

0

我在找东西的时候来到了这个话题。三年后,这个问题仍未得到答复。

json_encode接受选项作为第二个参数。如果您按照以下方式使用它,特殊字符将显示为原始。

json_encode($data, JSON_UNESCAPED_UNICODE) 
相关问题