How to convert HTML entities like – to their character equivalents?

爷,独闯天下 提交于 2019-12-17 18:21:35

问题


I am creating a file that is to be saved on a local user's computer (not rendered in a web browser).

I am currently using html_entity_decode, but this isn't converting characters like – (which is the n-dash) and was wondering what other function I should be using.

For example, when the file is imported into the software, instead of the ndash or just a - it shows up as –. I know I could use str_replace, but if it's happening with this character, it could happen with many others since the data is dynamic.


回答1:


You need to define the target character set. – is not a valid character in the default ISO-8859-1 character set, so it's not decoded. Define UTF-8 as the output charset and it will decode:

echo html_entity_decode('–', ENT_NOQUOTES, 'UTF-8');

If at all possible, you should avoid HTML entities to begin with. I don't know where that encoded data comes from, but if you're storing it like this in the database or elsewhere, you're doing it wrong. Always store data UTF-8 encoded and only convert to HTML entities or otherwise escape for output when necessary.




回答2:


Try mb_convert_encoding():

$string = "n–dash";
$output = mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES');
echo $output;



回答3:


UPDATE

function decode_characters($data)
{
    $text = $data;
    $enc = mb_detect_encoding($text, "UTF-8,ISO-8859-1");
    $resutl_characters = iconv($enc, "UTF-8", $text);
    return $resutl_characters;
}



回答4:


Encode the file as UTF-8 using utf8_encode(). Then you don't have to replace/remove anything.




回答5:


Are you trying to turn the characters into HTML Entities for storage and later retrieval?

htmlentities('–', ENT_COMPAT, 'UTF-8');
// Returns "–"

If I have misread your question, please let me know.



来源:https://stackoverflow.com/questions/4880930/how-to-convert-html-entities-like-8211-to-their-character-equivalents

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