php – Charset编码问题

我正在开发一个阿拉伯语网站.但是,我使用AJAX在我的数据库中保存了一些文本. AJAX和我一起工作得很好.我的问题是,当我将数据保存在我的数据库中并尝试在我的屏幕上打印时,它会返回一个奇怪的文本.我使用PHP函数mb_detect_encoding来确定数据库如何处理文本.该函数返回UTF-8.
所以我使用iconv(“windows-1256”,“UTF-8”,$row [“text”])在屏幕上打印文本,但它仍然返回这个奇怪的东西.请伸出援助之手
谢谢
最佳答案
请查看this主题(并在首先发布问题之前使用搜索).

在你的情况下,我认为你忘了为数据库连接设置错误的字符集(使用SET NAMES语句或mysql_set_charset()) – 但这很难说.

这是chazomaticus的引用,他在喜欢的主题中给出了一个完美的答案,列出了你需要关注的所有要点:

Storage:

  • 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.

Retrieval:

  • 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).

Delivery:

  • 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.

Submission:

  • 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.

Processing:

  • 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 07003 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.

转载注明原文:php – Charset编码问题 - 代码日志