[英]using strlen in php for form validation
I have the following validation: 我有以下验证:
if (empty($errors) === true) {
if (strlen($_POST['Description']) > 250) { //strlen counts the number of bytes
$errors[] = 'Your description must be less than 250 characters long';
}
}
But if a user puts in a description more than 30 characters long this error will always be displayed. 但是,如果用户输入的描述超过30个字符,则会始终显示此错误。 I have read that strlen returns the number of bytes rather than the number of characters in a string.
我读过strlen返回的是字节数,而不是字符串中的字符数。 I have tried mb_strlen to get character length but this doesn't work either.
我尝试了mb_strlen来获取字符长度,但这也不起作用。 I feel like i am doing something obviously stupid, please help if you can.
我觉得我做的事情显然很愚蠢,如果可以的话请帮助。
Honestly i do not know of any encoding using 8 bytes for a character. 老实说,我不知道使用8个字节的字符进行任何编码。
I think you "$_POST['Description']" may not contain only the data you want. 我认为您“ $ _POST ['Description']”可能不只包含您想要的数据。
Do a 做一个
var_dump($_POST['Description'])
And see what it shows. 并查看显示的内容。
on a sidenote: 在旁注:
if (strlen(trim($_POST['Description'])) > 250)
prevents whitespace to cause validation to fail. 防止空格导致验证失败。
Also note that you will have to specify the encoding to mb_strlen as the encoding of characters sent might not be the same as internal encoding. 另请注意,您必须将编码指定为mb_strlen,因为发送的字符编码可能与内部编码不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.