php Codeigniter 表单验证规则 - 修剪有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3537043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Codeigniter form validation rules - What does trim do?
提问by pixeltocode
What does the trim form validation rule actually do and when should I use it?
修剪表单验证规则实际上有什么作用,我应该什么时候使用它?
Thanks
谢谢
回答by Stephen Curran
It removes whitespace from the beginning and end of an input string.
它从输入字符串的开头和结尾删除空格。
You can think of validation rules as a pipeline of routines run on the input. For example :
您可以将验证规则视为在输入上运行的例程管道。例如 :
$this->form_validation->set_rules('email', 'email', 'trim|required|valid_email');
When validating the email input CodeIgniter :
验证电子邮件输入 CodeIgniter 时:
- Removes whitespace from the beginning and end of the string
- Checks that the resulting string is non-empty
- Checks that it is a valid email address
- 从字符串的开头和结尾删除空格
- 检查结果字符串是否为非空
- 检查它是否是有效的电子邮件地址
The trim routine is normally run before other routines and placed at the front of the list.
修剪例程通常在其他例程之前运行并放在列表的前面。
You should use it when you want to ignore the starting and trailing whitespace in the input. Perhaps the user has accidentally typed a space character after an email address in a HTML form. The space character should not be taken as part of the email address and should be removed.
当您想忽略输入中的开头和结尾空格时,应该使用它。也许用户不小心在 HTML 表单中的电子邮件地址后键入了一个空格字符。空格字符不应作为电子邮件地址的一部分,应删除。
回答by Dwayne Charrington
You can use any in-built PHP function that takes one argument and returns a value or true or false as a form validation rule. So trim isn't actually a Codeigniter specific function, it's a PHP one.
您可以使用任何带有一个参数并返回一个值或 true 或 false 作为表单验证规则的内置 PHP 函数。所以trim 实际上并不是一个Codeigniter 特定的函数,它是一个PHP 函数。
Some other in-built PHP functions you can use are; is_int (checks if a number is an integer), ltrim (trims left of a string), rtrim (trims the right of a string), sha1, md5, etc.
您可以使用的其他一些内置 PHP 函数是;is_int(检查数字是否为整数)、ltrim(修剪字符串左侧)、rtrim(修剪字符串右侧)、sha1、md5 等。