本文介绍了如何转换HTML实体(如–到他们的性格等同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个文件保存在本地用户的计算机上(不在网络浏览器中呈现)。

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

我目前使用 html_entity_decode ,但这不会转换像– (这是n-dash)的字符,函数我应该使用。

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.

例如,当文件导入到软件,而不是ndash或只是一个 - 它显示为– 。我知道我可以使用 str_replace ,但如果它发生在这个字符,它可能发生与许多其他,因为数据是动态的。

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.

推荐答案

您需要定义目标字符集。 – 不是默认ISO-8859-1字符集中的有效字符,因此不会解码。定义UTF-8作为输出字符集,它将解码:

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');

如果可能,您应避免使用HTML实体。我不知道编码数据来自哪里,但如果你像这样存储在数据库或其他地方,你做错了。始终存储UTF-8编码的数据,并且只转换为HTML实体或在必要时转义以输出。

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实体(如–到他们的性格等同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:02