字符集编码问题

编程入门 行业动态 更新时间:2024-10-27 13:20:51
本文介绍了字符集编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个阿拉伯网站.但是,我使用AJAX在数据库中保存了一些文本. AJAX对我来说很好用.我的问题是,当我将数据保存在数据库中并尝试在屏幕上打印时,它返回了一个奇怪的文本.我已经使用PHP函数mb_detect_encoding来确定数据库如何处理文本.该函数返回了UTF-8. 所以我使用了iconv("windows-1256","UTF-8",$ row ["text"])在屏幕上打印文本,但是它仍然返回这个奇怪的东西.请帮忙 谢谢

I am developing an Arabic web site. However, I use AJAX to save some text in my data base. The AJAX works fine with me. My problem is, when I save the data in my database and try to print it on my screen, it returns a weird text. I have used the PHP function mb_detect_encoding to determine how the database deals with the text. The function returned UTF-8. So I used iconv("windows-1256","UTF-8",$row["text"]) to print the text on the screen, but it still returning this weird thing. Please give a hand Thanks

推荐答案

请查看此线程(和先搜索,然后再发布问题.

please take a look at this thread (and use the search before posting a question first).

在您的情况下,我认为您已经忘记为数据库连接设置最重要的字符集(使用SET NAMES语句或 mysql_set_charset())-但这很难说.

in your case, i think you've forgotten to set the chorrect charset for you database-connection (using a SET NAMES statement or mysql_set_charset()) - but thats hard to say.

这是来自 chazomaticus 的报价,他在喜欢的主题中给出了一个完美的答案,列出了您的所有要点必须照顾:

this is a quote from chazomaticus, who has given a perfect answer in the liked thread, listing all the points you have to care of:

存储空间:

Storage:

  • 指定utf8_unicode_ci(或 等价)排序在所有表上 和数据库中的文本列. 这使得MySQL实际存储和 本地检索UTF-8中的值.
  • Specify utf8_unicode_ci (or equivalent) collation on all tables and text columns in your database. This makes MySQL physically store and retrieve values natively in UTF-8.

检索:

  • 在PHP中,无论您使用哪种DB包装器 使用,您需要设置连接 utf8的字符集.这样,MySQL确实 没有从其本地UTF-8转换 当它将数据传递给PHP时. * 请注意,如果您不使用数据库 包装器,您可能必须发出 告诉MySQL给您的查询 结果为UTF-8:SET NAMES 'utf8' (一旦您连接).
  • In PHP, in whatever DB wrapper you use, you'll need to set the connection charset to utf8. This way, MySQL does no conversion from its native UTF-8 when it hands data off to PHP. * Note that if you don't use a DB wrapper, you'll probably have to issue a query to tell MySQL to give you results in UTF-8: SET NAMES 'utf8' (as soon as you connect).

送货:

  • 您必须告诉PHP提供 客户端的正确标头,因此 文本将被解释为UTF-8.在 PHP,可以使用default_charset php.ini选项,或手动发出 Content-Type自己标头,其中 只是更多的工作,但具有相同的功能 效果.
  • You've got to tell PHP to deliver the proper headers to the client, so text will be interpreted as UTF-8. In PHP, you can use the default_charset php.ini option, or manually issue the Content-Type header yourself, which is just more work but has the same effect.

提交:

  • 您希望所有数据通过以下方式发送给您 浏览器使用UTF-8. 不幸的是,唯一的方法 可靠地做到这一点是添加 accept-charset属性归您所有 <form>标签:<form ... accept-charset="UTF-8">.
  • 注意 W3C HTML规范指出 客户应该"默认发送 以任何形式返回服务器 字符集服务的服务器,但这是 显然只有一个建议, 因此需要明确 每个<form>标签.
  • 尽管在那方面,您仍然 想要验证每个提交的字符串 作为有效的UTF-8,然后再尝试 存储或在任何地方使用. PHP的 mb_check_encoding()可以解决问题, 但您必须认真使用它.
  • You want all data sent to you by browsers to be in UTF-8. Unfortunately, the only way to reliably do this is add the accept-charset attribute to all your <form> tags: <form ... accept-charset="UTF-8">.
  • Note that the W3C HTML spec says that clients "should" default to sending forms back to the server in whatever charset the server served, but this is apparently only a recommendation, hence the need for being explicit on every single <form> tag.
  • Although, on that front, you'll still want to verify every submitted string as being valid UTF-8 before you try to store it or use it anywhere. PHP's mb_check_encoding() does the trick, but you have to use it religiously.

处理:

  • 不幸的是,这很难 部分.您需要确保 每次您处理UTF-8字符串时, 您这样做很安全.最简单的方法 这是通过广泛使用 PHP的mbstring扩展名.
  • PHP的 字符串操作默认情况下不是 UTF-8安全.你有些事 可以安全地使用普通的PHP字符串 操作(例如串联),但是 对于大多数事情,您应该使用 等效的mbstring函数.
  • 至 知道你在做什么(阅读:不要乱七八糟 它),您真的需要了解UTF-8 以及最低的工作方式 可能的水平.签出任何 来自 utf8 的链接 资源来学习您需要的一切 要知道.
  • 我也这样 应该在某个地方说,即使 看起来似乎很明显:每个PHP或HTML 您要提供的文件应该是 使用有效的UTF-8编码.
  • This is, unfortunately, the hard part. You need to make sure that every time you process a UTF-8 string, you do so safely. Easiest way to do this is by making extensive use of PHP's mbstring extension.
  • PHP's string operations are NOT by default UTF-8 safe. There are some things you can safely do with normal PHP string operations (like concatenation), but for most things you should use the equivalent mbstring function.
  • To know what you're doing (read: not mess it up), you really need to know UTF-8 and how it works on the lowest possible level. Check out any of the links from utf8 for some good resources to learn everything you need to know.
  • Also, I feel like this should be said somewhere, even though it may seem obvious: every PHP or HTML file you'll be serving should be encoded in valid UTF-8.

请注意,您不需要使用utf-8-重要的部分是在任何地方都使用相同的字符集,而与可能使用的字符集无关.但是如果仍然需要进行更改,请使用utf-8.

note that you don't need to use utf-8 - the important part is to use the same charset everywhere, independent of what charset that might be. but if you need to change things anyway, use utf-8.

更多推荐

字符集编码问题

本文发布于:2023-05-27 19:05:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/299884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符集

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!