$ _GET变量与乱码编码 [英] $_GET variable with messed up encoding

查看:115
本文介绍了$ _GET变量与乱码编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上编码很麻烦。



这是我现在的问题,如果我去 analize.php?dialog =árbol哪个代码是:

  < 
echo $ _GET ['dialog'];
echosabía;

我得到:

 sabÃa
sabía

我正在使用ANSI, UTF-8打破了两者。我不明白为什么会发生这种情况,上面没有任何代码。我不在乎它们如何显示,因为该文件仅用于从我的数据库中获取数据。但是我需要使 $ _ GET 正确显示,以便我可以将其包含在查询中。



完成?

解决方案

您不能在URL中发送字符í,URL必须使用ASCII字符集的子集。因此,在发送到服务器之前,您的浏览器将该URL编码为?dialog = sab%C3%ADa %C3%AD 表示两个字节 C3 AD ,这是字符í的UTF-8编码。您可以使用 var_dump($ _ SERVER ['QUERY_STRING']); 确认。这由PHP自动解码,结果是sabía的UTF-8字节序列,í使用两个字节 C3 AD 进行编码。



您的浏览器正在使用Windows-1252或ISO-8859-1字符集解释此字节序列。字节 C3 表示此编码中的Ã,字节 AD 表示软连字符,不可见。 / p>

两种可能的解决方案:


  1. 使用UTF-8(推荐




    • 将源代码保存为UTF-8

    • 输出一个标题强制浏览器将网站解释为UTF-8:

        header('Content-Type:text / html; charset = utf -8' ); 



  2. 转换<$ c $使用 mb_convert_encoding 或者Windows $ 1252 / ISO-8859-1(或您要在您的网站上使用的任何编码) iconv (不推荐)




    • 即使在这种情况下,您应该设置一个标题向浏览器宣传您正在使用的编码


总之,你需要确保你使用相同的编码,并指定浏览器是什么编码。


I'm having a great deal of trouble with encoding in my site.

This is my problem right now, if I go to analize.php?dialog=árbol which code is:

<?
echo $_GET['dialog'];
echo "sabía";

on it I get:

sabía
sabía

I'm using ANSI, changing to UTF-8 breaks both. I don't understand why this happens, also there isn't any code above this. I don't care about how they display since this file is only used to fetch data from my database. But I need to make $_GET display properly so I can include it on the query.

How can this be done?

解决方案

You cannot send the character "í" in a URL, URLs must use a subset of the ASCII charset. Therefore the URL is encoded to ?dialog=sab%C3%ADa by your browser before being sent to the server. %C3%AD represents the two bytes C3 AD, which is the UTF-8 encoding for the character "í". You can confirm this with var_dump($_SERVER['QUERY_STRING']);. This is automatically decoded by PHP, the result is the UTF-8 byte sequence for "sabía" with the "í" being encoded using the two bytes C3 AD.

Your browser is interpreting this byte sequence using the Windows-1252 or ISO-8859-1 charset. The byte C3 represents "Ã" in this encoding, the byte AD represents a soft-hyphen and is invisible.

Two possible solutions:

  1. use UTF-8 everywhere (recommended!)

    • save your source code as UTF-8
    • output a header that forces the browser to interpret the site as UTF-8:

      header('Content-Type: text/html; charset=utf-8');
      

  2. convert the $_GET values to Windows-1252/ISO-8859-1 (or whatever encoding you want to use on your site) using mb_convert_encoding or iconv (not recommended)

    • even in this case you should set a header that announces to the browser what encoding exactly you're using

In short, you need to make sure you're using the same encoding everywhere and specify to the browser what encoding exactly that is.

这篇关于$ _GET变量与乱码编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆