PHP £ 之前有奇怪的字符符号?
由于某种原因,当我在表单上的文本字段中输入 £ 时,我收到了 Â
£76756687 奇怪的字符?
For some reason i get a Â
£76756687 weird character when i type a £ into a text field on my form?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所怀疑的,这是一个字符编码问题 - 页面是否设置为使用 UTF-8 字符集? (这种编码确实不会出错。)此外,您可能希望在退出时对英镑符号进行实体编码 (
£
)作为示例字符集(例如表单页面和 HTML 电子邮件)您可以使用:
也就是说,用户必须输入货币符号是否有充分的理由?将其作为静态文本项或文本字段左侧的下拉菜单会是更好的主意吗? (如果我在胡说八道,而你正在使用自由格式的文本区域或摘要,请随意忽略。)
As you suspect, it's a character encoding issue - is the page set to use a charset of UTF-8? (You can't go wrong with this encoding really.) Also, you'll probably want to entity encode the pound symbol on the way out (
£
)As an example character set (for both the form page and HTML email) you could use:
That said, is there a good reason for the user to have to enter the currency symbol? Would it be a better idea to have it as either a static text item or a drop down to the left of the text field? (Feel free to ignore if I'm talking arse and you're using a freeform textarea or summat.)
您可能使用 UTF-8 作为字符编码,但没有正确声明您的输出。因为
£
字符 (U+00A3) 在 UTF-8 中编码为 0xC2A3。当用 ISO 8859-1 解释时,该字节序列表示两个字符Â
和£
。所以你只需要正确指定你的字符编码即可。在 PHP 中,您可以使用
header
函数 为 Content-Type 标头字段 例如:但是确保在任何输出之前调用此函数。否则 HTTP 标头已发送,您无法修改它。
You’re probably using UTF-8 as character encoding but don’t declare your output correctly. Because the
£
character (U+00A3) is encoded in UTF-8 with 0xC2A3. And that byte sequence represents the two charactersÂ
and£
when interpreted with ISO 8859-1.So you just need to specify your character encoding correctly. In PHP you can use the
header
function to set the proper value for Content-Type header field like:But make sure that you call this function before any output. Otherwise the HTTP header is already sent and you cannot modify it.