警告:preg_match()[function.preg-match]:找不到结尾的分隔符'/' [英] Warning: preg_match() [function.preg-match]: No ending delimiter '/' found
问题描述
警告:preg_match()[function.preg-match]:没有结尾分隔符'/'
这是检查字母数字输入的功能
function CheckAlphanumeric($ element,$ minlength,$ maxlength)
{如果(!preg_match(/ [^ A-Za-z\s\0-9 - @。] //,$ element)&& strlen($ element)> = $ minlength&& ; strlen($ element)< = $ maxlength){return TRUE; }
else {return FALSE;}
}
你对这个警告的投入吗?
你会想逃避最后一个斜杠(或删除它,不知道是否应该在正则表达式中? )。此外,您在0之前有一个反斜杠,这也没有什么意义。
#如果它应该是在那里。
/ [^ A-Za-z\s0-9 - @。] \ //
或
#使用不同的字符作为分隔符
%[^ A-Za-z\\ \\s0-9 - @。] /%
或
#删除它,如果它是一个错字!
/ [^ A-Za-z\s0-9 - @。] /
另外,你知道你可以使用 [:alnum:]
快捷方式吗? ( src )
#匹配字母数字, - ,@和。
/[[alalum:]-@.]/
希望最终编辑:
我建议你先看看你的功能,这有点混乱。你本质上要检查三个条件,1)它是否通过正则表达式,2)是最小长度,3)是最大长度。由于所有三个返回布尔值(或正确评估为布尔值的东西),您可以简化函数如下:
函数CheckAlphanumeric($ element,$ minlength,$ maxlength){
//如果匹配所有条件,则返回TRUE,如果失败则返回FALSE。
return preg_match(/ [[:alnum:] - @。] /,$ element)&& strlen($ element)> = $ minlength&&& strlen($ element)< = $ maxlength;
}
when this function was called it gave the message Warning: preg_match() [function.preg-match]: No ending delimiter '/'
this is the function to check for alphanumeric input
function CheckAlphanumeric($element,$minlength,$maxlength)
{ if (!preg_match ("/[^A-Za-z\s\0-9 - @ .]//", $element) && strlen($element)>=$minlength && strlen($element) <=$maxlength) { return TRUE; }
else { return FALSE;}
}
What are your inputs on this warning?
You'll want to escape the last forward slash (or remove it, not sure if it should be in that regex?). Also, you have a backslash before the 0 which doesn't really make sense either.
# escape it if it's supposed to be in there.
/[^A-Za-z\s0-9 - @ .]\//
or
# use a different character as a delimiter
%[^A-Za-z\s0-9 - @ .]/%
or
# remove it if it's a typo!
/[^A-Za-z\s0-9 - @ .]/
Also, did you know you can use a [:alnum:]
shortcut? (src)
# matches alpha numeric, "-", "@", and "."
/[[:alnum:]-@.]/
Hopefully final edit:
I'd suggest you take a look at your function in the first place too, it's a bit confusing. You essentially want to check against three conditions, 1) does it pass regex, 2) is it gte the min length, and 3) is it lte the max length. Since all three of these return booleans (or things that evaluate correctly as booleans), you can simplify the function as follows:
function CheckAlphanumeric($element,$minlength,$maxlength) {
// returns TRUE if it matches all conditions, FALSE if one fails.
return preg_match("/[[:alnum:]-@. ]/", $element) && strlen($element) >= $minlength && strlen($element) <= $maxlength;
}
这篇关于警告:preg_match()[function.preg-match]:找不到结尾的分隔符'/'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!