简体   繁体   English

PHP,使用 UTF8 进行表单检查

[英]PHP, doing form checks with UTF8

So to support multilanguage on the site I need to make sure all data going into the database is stored as utf8.因此,为了在网站上支持多语言,我需要确保进入数据库的所有数据都存储为 utf8。 My question is, is there a class out there that does form checks and data sanitization for forms using utf8 formatted form data?我的问题是,是否有一个 class 使用 utf8 格式的表单数据对 forms 进行表单检查和数据清理? Right now I do checks such as if empty, or if the form data is a certain length, but would have to use different commands because of utf8.现在我做检查,比如是否为空,或者表单数据是否有一定的长度,但由于 utf8 必须使用不同的命令。 So just wanted to check and see if there is a pre-existing class for this type of checking/sanitizing as I try not to re-invent the wheel.所以只是想检查一下是否有一个预先存在的 class 用于这种类型的检查/消毒,因为我尽量不重新发明轮子。

My question is, is there a class out there that does form checks and data sanitization for forms using utf8 formatted form data?我的问题是,是否有一个 class 使用 utf8 格式的表单数据对 forms 进行表单检查和数据清理?

This shouldn't be necessary in the first place.首先这不应该是必要的。 If your form (and everything else along the way) is properly encoded as UTF-8, there should be no hiccups.如果您的表单(以及沿途的所有其他内容)正确编码为 UTF-8,则应该没有问题。

If everything is properly set up, only thing where things can go wrong is when the user enters invalid characters into the form.如果一切设置正确,只有当用户在表单中输入无效字符时才会出错。 It's impossible to reliably defend against that - but the risk of this happening is minimal.不可能可靠地防御这种情况——但发生这种情况的风险很小。

If you have a real-world situation where invalid characters can make it into the data, you can do an iconv() with the //IGNORE option to weed out invalid characters:如果在实际情况中无效字符可以进入数据,您可以使用//IGNORE选项执行iconv()来清除无效字符:

$data = iconv("UTF-8", "UTF-8//IGNORE", $data);

the same way, you can find out whether a string contains invalid characters by comparing string lengths before and after the iconv().同样的方法,你可以通过比较 iconv() 前后的字符串长度来判断一个字符串是否包含无效字符。

Some general information about the topic can be found in Building Scalable Web Sites by Cal Hernderson (O'Reilly 2006) in chapter 5 ( Chapter 5 as PDF ).有关该主题的一些一般信息可以在 Cal Hernderson 的Building Scalable Web 站点(O'Reilly 2006)第 5 章(第 5 章为 PDF )中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM