$ _POST将从utf-8转换为äö¼等 [英] $_POST will convert from utf-8 to ä ö ü etc

查看:242
本文介绍了$ _POST将从utf-8转换为äö¼等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里是新的,所以如果我做错了什么,我道歉。



我有一个表单提交用户输入到另一个页面。用户需要键入ä,ö,é等...我已将以下所有内容放在文档中:

  < meta http-equiv =Content-Typecontent =text / html; charset = UTF-8> 
header('Content-Type:text / html; charset = UTF-8');
< form action =whatever.phpaccept-charset =UTF-8>

我甚至试过:

  ini_set('default_charset','UTF-8'); 

加载其他页面时,我需要检查用户输入的内容,如:

  if($ _POST ['field'] == $ check){
...
}

但是如果他输入München,PHP会比较'München'和'München'即使它应该触发TRUE。由于它被指定为UTF-8无处不在,我猜,服务器正在转换为别的东西(我在另一个线程上读取的Windows-1252),因为它不支持或没有配置为UTF-8。我在本地服务器上使用Apache之前我加载到生产;我没有改变(和不知道如何)任何默认设置。我一直在一个Windows 7上工作,用Notepad ++编辑我的文件在ANSI。如果我 bin2hex('München')我得到'4dc3bc6e6368656e'。



如果我 echo $ _POST ['field']; 正确显示München。



我已经研究了无处不在的解释,



任何帮助都非常感激。

解决



问题1)你说的是 echo $ _POST ['field']; 会正确显示它吗? 显示是什么意思?它可以在两种情况下正确显示:




  • 字段采用UTF-8格式,并且您的页面已声明为UTF-浏览器将其显示为UTF-8或

  • 该字段为拉丁语-1,并且浏览器已通过自动检测启发式决定您的网页是拉丁语-1 。



因此, echo $ _POST ['field'];

问题2)您正在使用

 < meta http-equiv =Content-Typecontent =text / html; charset = UTF-8/> 
header('Content-Type:text / html; charset = UTF-8');

这是PHP代码吗?如果是,它将是一个错误,因为头必须在发出任何字节之前设置。如果这样做,您将不会设置 Content-Type 头,PHP应该生成警告。



问题3 )您正在使用

 < form action =whatever.phpaccept-charset =UTF-8> 

某些浏览器(大多数情况下为IE)忽略 accept-charset 如果他们可以胁迫要发送的数据以ASCII或ISO Latin-1。因此,数据将是UTF-8并声明为ISO Latin-1或ISO Latin-1,并以ISO Latin-1格式发送(但第二种情况不是这种情况)。



请查看 http://stackoverflow.com/a/8547004/449288 了解如何解决这个问题。



问题4)你比较哪些字符串?例如,如果您有

  $ city =München
$ _POST ['city'] == $ city

此代码的结果将取决于PHP文件的编码。如果文件以ISO Latin-1编码,并且 $ _ POST 正确包含UTF-8数据,则 == 将比较不同的字节并返回false。


I am new here, so I apologize if I am doing anything wrong.

I have a form which submits user input onto another page. User is expected to type ä, ö, é, etc... I have placed all of the following in the document:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
header('Content-Type:text/html; charset=UTF-8');
<form action="whatever.php" accept-charset="UTF-8">

I even tried:

ini_set('default_charset', 'UTF-8');

When the other page loads, I need to check what the user input with something like:

if ( $_POST['field'] == $check ) {
  ...
}

But if he inputs something like 'München', PHP will compare 'München' with 'München' and will never trigger TRUE even though it should. Since it is specified UTF-8 everywhere, I am guessing that the server is converting to something else (Windows-1252 as I read on another thread) because it does not support or is not configured to UTF-8. I am using Apache on a local server before I load into production; I have not changed (and don't know how to) any of the default settings. I've been working on a Windows 7, editing with Notepad++ enconding my files in ANSI. If I bin2hex('München') I get '4dc3bc6e6368656e'.

If I echo $_POST['field']; it displays 'München' correctly.

I have researched everywhere for an explanation, all I find is that I should include those tags/headings I already have.

Any help is much appreciated.

解决方案

You are facing many different problems at the same, let's start with the simplest one.

Problem 1) You say that echo $_POST['field']; will display it correctly? What do you mean with "display"? It can be displayed correctly in two cases:

  • either the field is in UTF-8 and your page has been declared as UTF-8 and the browser is displaying it as UTF-8 or,
  • the field is in Latin-1 and the browser has decided (through the auto-detection heuristics) that your page is in Latin-1.

So, the fact that echo $_POST['field']; is correct tells you nothing.

Problem 2) You are using

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
header('Content-Type:text/html; charset=UTF-8');

Is this PHP code? If it is, it will be an error because the header must be set before sending out any byte. If you do this you will not set the Content-Type header and PHP should generate a warning.

Problem 3) You are using

<form action="whatever.php" accept-charset="UTF-8">

Some browsers (IE, mostly) ignore accept-charset if they can coerce the data to be sent in ASCII or ISO Latin-1. So the data will be in UTF-8 and declared as ISO Latin-1 or ISO Latin-1 and sent as ISO Latin-1 (but this second case is not your case).

Have a look at http://stackoverflow.com/a/8547004/449288 to see how to solve this problem.

Problem 4) Which strings are you comparing? For example, if you have

$city = "München"
$_POST['city'] == $city

The result of this code will depend on the encoding of the PHP file. If the file is encoded in ISO Latin-1 and the $_POST correctly contains UTF-8 data, the == will compare different bytes and will return false.

这篇关于$ _POST将从utf-8转换为äö¼等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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