PHP 中的 html_entity_decode 问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4638611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
html_entity_decode problem in PHP?
提问by mootymoots
I am trying to convert HTML entities from a source string to their literal character equivalent.
我正在尝试将 HTML 实体从源字符串转换为其等效的文字字符。
For example:
例如:
<?php
$string = "Hello – World";
$converted = html_entity_decode($string);
?>
Whilst this rightly converts the entity on screen, when I look at the HTML code it is still showing the explicit entity. I need to change that so that it literally converts the entity as I am not using the string within an HTML page.
虽然这正确地转换了屏幕上的实体,但当我查看 HTML 代码时,它仍然显示显式实体。我需要更改它,以便它从字面上转换实体,因为我没有在 HTML 页面中使用字符串。
Any ideas on what I am doing wrong?
关于我做错了什么的任何想法?
FYI I am sending the converted string to Apple's Push notification service:
仅供参考,我将转换后的字符串发送到 Apple 的推送通知服务:
$payload['aps'] = array('alert' => $converted, 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);
回答by BoltClock
–
maps to a UTF-8 character (the em dash) so you need to specify UTF-8 as the character encoding:
–
映射到 UTF-8 字符(破折号),因此您需要指定 UTF-8 作为字符编码:
$converted = html_entity_decode($string, ENT_COMPAT, 'UTF-8');
回答by mr.Shu
Try using charset
尝试使用字符集
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<?php
$string = "Hello – World";
$converted = html_entity_decode($string , ENT_COMPAT, 'UTF-8');
echo $converted;
?>
This should work And it should be converted also in the source
这应该有效而且它也应该在源代码中进行转换