PHP $ _POST不需要重音 [英] PHP $_POST doesn't take accents
问题描述
这个问题已解决,请查看我的答案以查看解决方案 m试图通过HTML提交带有重音字母的文本到POST页面,问题是重音字母被转换为不可读的字符。
p>我有这样的表单:
< form action =page.phpmethod =POST>
< input type =textareaid =text1name =text1/>
< input type =submitvalue =Send/>
< / form>
然后,在page.php中:
echo $ _POST ['text1'];
问题是如果我输入àèìòù
在我的textarea然后我提交表单,我得到这个输出:
ÃèìòùÃ$
我的下一步是将带重音的字母转换为带有 htmlentities($ _ POST ['text1' ]
但我需要 $ _ POST
给我正确的字母。
在页头中,我已经有了
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
如何解决这个问题?
编辑
我尝试过
< / form>
< / code>
并没有解决它
编辑2
还尝试添加
< meta charset ='utf-8'>
添加到我的文档头部,并且不起作用
编辑3
我尝试在第二页上将我的字符集设置为UTF-8,并且
echo $ _POST ['text1'];
显示正确的结果。
我看到这个问题是我使用htmlentities,与此代码
echo htmlentities($ _ POST ['text1']);
我得到
&安培; Atilde;&安培; NBSP;&安培; Atilde;&安培; UML;&安培; Atilde;&安培;不;&安培; Atilde;&安培; SUP2;&安培; Atilde;&安培; SUP1;
实际产出
ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃ标签和标题了。有谁知道我该如何解决它?解决方案好的,我终于解决了它。即使我设置字符集,无论是用PHP头部设置它还是
header('Content-Type:text / html;字符集= UTF-8' );
或HTML元标记
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
并将我的文件保存为UTF-8,但它无效。
输入àèìòù
,然后使用 htmlentities
处理它总是导致
& Atilde;& nbsp;& Atilde;& uml;& Atilde;& not& Atilde;& amp ; SUP2;&安培; Atilde;&安培; SUP1;
在可读字符中,是:
ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃ改变了这个: echo htmlentities($ _ POST ['text1']); b
echo htmlentities($ _ POST ['text1'],ENT_QUOTES,UTF-8);
并且一切正常,我可以打印出真实的输入。
谢谢大家的帮助!
This question is solved, check my answer to see the solution
I'm trying to add to my DB a text with accented letters through an html form that submits with POST to a PHP page, the problem is that accented letters are converted to unreadable characters.
I have this form:
<form action="page.php" method="POST">
<input type="textarea" id="text1" name="text1" />
<input type="submit" value="Send" />
</form>
And then, in page.php:
echo $_POST['text1'];
The problem is that if i input àèìòù
in my textarea and then i submit the form, i get this output:
à èìòù
My next step would be convert accented letters to html entities with htmlentities($_POST['text1']
but i need $_POST
to give me the correct letters.
Note: in the page head i already have
<meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
How can i fix this?
EDIT
I tried with
<form action="page.php" method="POST" accept-charset="UTF-8">
<input type="textarea" id="text1" name="text1" />
<input type="submit" value="Send" />
</form>
And it didn't solve it
EDIT 2
Also tried with adding
<meta charset='utf-8'>
to my document's head, and it doesn't work
EDIT 3
I tried with setting my charset as UTF-8 on the second page too, and
echo $_POST['text1'];
displayed the correct result.
I saw that the problem is whe i use htmlentities, with this code
echo htmlentities($_POST['text1']);
I get
à èìòù
Which actually outputs
à èìòù
even if i set charset in my meta-tags and header too. Does anyone know how can i solve it?
解决方案 Ok, i finally solved it. Even if i was setting charset, no matter if setting it with PHP header
header('Content-Type: text/html; charset=utf-8');
or with HTML meta tag
<meta http-equiv = "Content-Type" content = "text/html; charset=utf-8" />
and saving my file as UTF-8, it didn't work.
Entering àèìòù
and then processing it with htmlentities
was always resulting in
à èìòù
That, in "readable" characters, is:
à èìòù
I just changed this:
echo htmlentities($_POST['text1']);
to this:
echo htmlentities($_POST['text1'], ENT_QUOTES, "UTF-8");
and everything worked, i get my real input printed out.
Thank you all for your help!
这篇关于PHP $ _POST不需要重音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!