问题
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?
回答1:
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;
}
回答2:
Well that's an odd error message for what you have there, but you have two /
at the end of your regex. You should only have one: "/[^A-Za-z\s\0-9 - @ .]/"
If you want to match a literal /
when you are using it as your delimiter, you need to escape it with \
.
来源:https://stackoverflow.com/questions/9689825/warning-preg-match-function-preg-match-no-ending-delimiter-found