html_entity_decode problem in PHP?

倾然丶 夕夏残阳落幕 提交于 2020-01-20 18:36:25

问题


I am trying to convert HTML entities from a source string to their literal character equivalent.

For example:

<?php

$string = "Hello &#8211; 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.

Any ideas on what I am doing wrong?

FYI I am sending the converted string to Apple's Push notification service:

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

回答1:


&#8211; maps to a UTF-8 character (the em dash) so you need to specify UTF-8 as the character encoding:

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



回答2:


Try using charset

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

This should work And it should be converted also in the source



来源:https://stackoverflow.com/questions/4638611/html-entity-decode-problem-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!