<head>
<meta charset="ISO-8859-7">
</head>
我一直在处理表单,发现
<meta charset="ISO-8859-7">
标签编码文本框内的文本。但存储文件所使用的编码方法不同。如果输入的字符不是由
<meta charset="ISO-8859-7">
指定的编码规范之一,该字符将被引用(如referenced(&#D;))。我原本以为表单会发送由指定编码生成的字节序列,因为无论我输入什么字符,它都会被一个编码解释成一个字节。例如,在
<meta charset="ISO-8859-7">
下,我在表单中输入字符 "¥"
。这个字符不属于该编码方式,但必须作为其代表的位置的字节发送
A5
,不管它是否可以被表示(这通常由任何编辑器完成)。但是,表单不会将其发送为字节,而是将字符引用。
代码:
index.php:
<?php header('Content-Type: text/html; charset=ISO-8859-7'); ?>
<head>
<meta charset="ISO-8859-7">
</head>
<form method="post" action="encode.php" accept-charset="ISO-8859-7">
<p><textarea name="input" maxlength="10" rows="5" cols="100"></textarea></p>
<p><button>Submit</button></p>
</form>
encode.php:
<head>
<meta charset="ISO-8859-7"><!-- Useless, Even if is specified the ISO-8859-1 where the "¥" exist, the form sended a reference char rather an a byte to interpret.-->
</head>
<?php
$input=$_POST["input"];
var_dump($input);
?>
源代码中的结果:
string(6) "¥"
注意:我已经测试了更改用于存储文件的编码。
在 index.php 中: 无论使用哪种编码来存储文件,表单始终会根据
accept-charset=""
属性或 <meta charset="">
标签发送。而对于 encode.php: 该字符串永远不会被文件编码。可以处理和表示,但是用于存储文件的编码与此无关。
Content-Type
头部信息是否可能发送了冲突的字符集? - cmbuckley<?php header('Content-Type: text/html; charset=ISO-8859-7'); ?>
,但仍然是一样的。 - nEAnnamenctype="multipart/form-data"
也不要漏掉。 - cmbuckley