【问题标题】:html_entity_decode problem in PHP?PHP中的html_entity_decode问题?
【发布时间】:2011-06-06 00:48:44
【问题描述】:

我正在尝试将 HTML 实体从源字符串转换为等效的文字字符。

例如:

<?php

$string = "Hello &#8211; World";
$converted = html_entity_decode($string);

?>

虽然这正确地转换了屏幕上的实体,但当我查看 HTML 代码时,它仍然显示显式实体。我需要更改它,以便它在我没有在 HTML 页面中使用字符串时从字面上转换实体。

关于我做错了什么有什么想法吗?

仅供参考,我将转换后的字符串发送到 Apple 的推送通知服务:

$payload['aps'] = array('alert' => $converted, 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);

【问题讨论】:

  • 说实话,回声线是无关紧要的。 $converted 中仍然有实体(我通过 API 发送转换为 iPhone)。
  • 是的,我想;那不是问题。我已经提供了答案。
  • 不带参数,只转换 > &返回。

标签: php html-entities html-encode


【解决方案1】:

&amp;#8211; 映射到 UTF-8 字符(破折号),因此您需要指定 UTF-8 作为字符编码:

$converted = html_entity_decode($string, ENT_COMPAT, 'UTF-8');

【讨论】:

  • 当我在那个实体上查看源代码时,我仍然得到实体...?
  • @mootymoots:我测试过,我得到的是原始字符而不是实体。想知道还有什么可能导致它...可能是 HTML 文档的编码?
  • 它是在页面上转换的 - 但不是在源代码中...?在 chrome 中查看
  • 补充一下,PHP是通过json_encode发送给苹果的,其实不需要在浏览器中查看,只是帮我调试。它作为设备上的实体出现。
  • 删除该评论,您正在使用 APNS。这意味着您的警报视图也显示&amp;#8211;,对吗?
【解决方案2】:

尝试使用字符集

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<?php
$string = "Hello &#8211; World";
$converted = html_entity_decode($string , ENT_COMPAT, 'UTF-8');
echo $converted;
?>

这应该可以 并且它也应该在源中进行转换

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多