如何正确编码/解码通过POST发送的变音符号 [英] How to properly encode/decode diacritics sent through POST

查看:114
本文介绍了如何正确编码/解码通过POST发送的变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,让我头疼了一阵子。我需要我的网络应用程序来正确处理任何东欧变音符号(主要是ăĂÎşşŞţŢŢ)。数据库正确处理它们,并且php脚本正确处理它们(所有php脚本使用utf-8编码),但是在处理表单时有问题。



例如,将以下内容插入到数据库中,它被正确处理,字符编码正确,保存正确,正确显示:

  $ insert_value = htmlentities('ăĂîÎşŞţŢâ',ENT_COMPAT,UTF-8); 

但是,如果在文本/文本区域输入中输入相同的字符串,并使用POST发送:

  $ insert_value = htmlentities($ _ POST ['test_string'],ENT_COMPAT,UTF-8); 

这将返回一个空字符串。



我检查了脚本使用Firefox接收到的POST,而不是'şŞţŢ'字符,我得到了奇怪的符号(ºªþÞ),所以我猜这就是为什么htmlentities返回空白。 >

我尝试在表单上设置enctype以进行多部分/表单数据,并且似乎正确地发送它,但是然后将htmlentities应用到它仍然以某种方式拧紧它(例如,如果 $ _ POST ['ta'] =ăĂîÎşŞţŢâ,htmlentities($ _ POST ['ta'],ENT_COMPAT,UTF-8)返回$ kappa;



编辑:非常感谢欺骗,该链接帮助我处理我的Web应用程序的编码,而无需任何htmlentities调用。我推荐这篇文章给任何人在php,html和数据库之间玩弄变音的麻烦。

解决方案

帮助...



不要对进入数据库的数据进行HTML编码,处理Unicode前端到Web应用程序。有一些这样的问题,前面提到的文章是最大的。


Here's my problem that has been giving me a headache for a while. I need my web application to properly handle any eastern european diacritics (mostly ă Ă î Î ş Ş ţ Ţ â Â). The database handles them properly, and the php scripts handle them properly (all php scripts use utf-8 encoding), however there is a problem when handling forms.

For example, insert the following to the database, it's handled correctly, characters are encoded properly, saved properly, displayed properly:

$insert_value = htmlentities('ă Ă î Î ş Ş ţ Ţ â Â',ENT_COMPAT, "UTF-8");

If, however, I type the same string in a text/textarea input and use POST to send it:

$insert_value = htmlentities($_POST['test_string'],ENT_COMPAT, "UTF-8");    

this will return an empty string.

I checked the the POST that the script is receiving with Firefox and instead of the 'ş Ş ţ Ţ' characters I get weird symbols(º ª þ Þ), so I'm guessing that's why htmlentities is returning empty.

I tried setting enctype on the form to multipart/form-data and it seems to send it properly but then applying htmlentities to it still somehow screws it up (for example if $_POST['ta'] = ăĂîÎşŞţŢâÂ, htmlentities($_POST['ta'],ENT_COMPAT, "UTF-8") returns $kappa;)

Edit: many thanks to deceze, that link helped me handle the encoding for my web application without any htmlentities calls. I recommend that article to anyone having trouble with juggling diacritics between php, html, and databases.

解决方案

Glad the comment helped...

Don't HTML-encode data that goes into the database, Handle Unicode Front To Back In A Web App. There are a number of gotchas for doing that, the aforelinked article goes through the biggest ones.

这篇关于如何正确编码/解码通过POST发送的变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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