php 如何转换像 – 这样的 HTML 实体 他们的性格等价物?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4880930/
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
How to convert HTML entities like – to their character equivalents?
提问by Cofey
I am creating a file that is to be saved on a local user's computer (not rendered in a web browser).
我正在创建一个要保存在本地用户计算机上的文件(不在 Web 浏览器中呈现)。
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.
我目前正在使用html_entity_decode
,但这并没有转换像–
(这是 n 破折号)这样的字符,并且想知道我应该使用什么其他功能。
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.
例如,当文件被导入软件时,而不是 ndash 或只是一个 - 它显示为–
. 我知道我可以使用str_replace
,但如果它发生在这个角色身上,那么它可能发生在许多其他角色身上,因为数据是动态的。
回答by deceze
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:
您需要定义目标字符集。–
不是默认 ISO-8859-1 字符集中的有效字符,因此不会对其进行解码。将 UTF-8 定义为输出字符集,它将解码:
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.
如果可能,您应该避免使用 HTML 实体。我不知道编码数据来自哪里,但是如果您像这样将其存储在数据库或其他地方,那么您就做错了。始终存储 UTF-8 编码的数据,并且仅在必要时转换为 HTML 实体或以其他方式转义以进行输出。
回答by Lèse majesté
Try mb_convert_encoding()
:
尝试mb_convert_encoding()
:
$string = "n–dash";
$output = mb_convert_encoding($string, 'UTF-8', 'HTML-ENTITIES');
echo $output;
回答by Jaydeep Gondaliya
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;
}
回答by ThiefMaster
Encode the file as UTF-8 using utf8_encode()
. Then you don't have to replace/remove anything.
使用 .UTF-8 将文件编码为 UTF-8 utf8_encode()
。然后你不必更换/删除任何东西。
回答by Luke Stevenson
Are you trying to turn the characters into HTML Entities for storage and later retrieval?
您是否试图将字符转换为 HTML 实体以供存储和以后检索?
htmlentities('–', ENT_COMPAT, 'UTF-8');
// Returns "–"
If I have misread your question, please let me know.
如果我误读了您的问题,请告诉我。