2012-08-26 10:18
浏览 62

从Percent Encoded URL参数中获取正确的UTF-8字符

I'm having some trouble with the dreaded UTF-8 Character Encoding! It's driving me insane, no matter which way I approach it or how many online guides I follow, I can never get it to return the desired results. Here's what's going on:

  • My whole website uses a simple text-file database that is UTF-8 encoded, and it correctly shows all manner of special characters, latin, arabic, japanese, you name it, they all show correctly, with one exception:

  • When the user uses the "Search" input box I have on my website, I use $search = $_REQUEST['search']; to get the input data on the results page and show results accordingly. When a user inserts special characters in the search box, they get "Percent Encoded" in the URL (for example, "ï" becomes "%E3%AF"). When showing $string in the actual website, any special character appears as � (black diamond with question mark).

  • I have tried everthing it says here http://malevolent.com/weblog/archive/2007/03/12/unicode-utf8-php-mysql/ with the exception of the header(). I have set the charset as UTF-8 in my head section with an http-equiv meta but for some reason whenever I set it as a header() my PHP stylesheet stops working (and the character problem remains). Maybe this is a clue?

  • I have tried urldecode and rawurldecode too, but they don't change anything.

  • Keep in mind special characters appear correctly elsewhere on the site, it's only with the $search string where this problem appears. As a side-note, even though the characters are not visualizing correctly, my search engine does actually interpret the special characters correctly when filtering the results. This makes me understand that the special character is actually there and correctly encoded, but it's just a matter of making it visualize correctly with the correct charset. However... everything appears to be UTF-8.

To be honest I'm so confused about this that this question might also appear to be confusing and the information I'm giving you might not be very well structured either, so I apologize and will try to provide more detailed information for any questions.

Thank you!

图片转代码服务由CSDN问答提供 功能建议

我在使用可怕的UTF-8字符编码时遇到了一些麻烦! 它让我疯狂,无论我接近它的方式或我遵循的在线指南有多少,我都无法让它返回所需的结果。 这是正在发生的事情:

  • 我的整个网站使用一个UTF-8编码的简单文本文件数据库,它正确显示所有特殊字符的方式 ,拉丁语,阿拉伯语,日语,你给它们命名,它们都正确显示,但有一个例外:

  • 当用户使用我在我的“搜索”输入框时 网站,我使用$ search = $ _REQUEST ['search']; 获取结果页面上的输入数据并相应地显示结果。 当用户在搜索框中插入特殊字符时,它们会在URL中显示“百分比编码”(例如,“ï”变为“%E3%AF”)。 在实际网站上显示$ string时,任何特殊字符都显示为�(带问号的黑色菱形)。

  • 我试过翻过它说这里 http://malevolent.com/weblog/archive/2007/03/ 12 / unicode-utf8-php-mysql / ,但header()除外。 我已经在我的头部区域中使用http-equiv元素将字符集设置为UTF-8,但出于某种原因,每当我将其设置为标题()时,我的PHP样式表停止工作(并且字符问题仍然存在)。 也许这是一个线索?

  • 我也尝试了urldecode和rawurldecode,但它们没有改变任何内容。

  • 继续 介意特殊字符在网站的其他位置正确显示,只有$ search字符串出现此问题。 作为旁注,即使字符无法正确显示,我的搜索引擎实际上在过滤结果时会正确解释特殊字符。 这让我明白特殊字符实际上存在并且编码正确,但这只是使用正确的字符集使其正确可视化的问题。 但是......一切似乎都是UTF-8。

    说实话我对此很困惑,这个问题也可能出现在 令人困惑,我给你的信息也可能不是很好,所以我道歉并会尝试为任何问题提供更详细的信息。


  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drrqwokuz71031449 2012-08-26 11:44

    Make sure not to have any function which alters your $_REQUEST. Some functions are not aware of special encodings.

    The best way to investigate is checking the state of the variables before and after they are altered.

    解决 无用
    打赏 举报

相关推荐 更多相似问题