php - How preg_match works exactly? -
I have written a simple task to check whether the sender string should be "valid" or not.
// This works without issues validate_email ($ value) {return preg_match ("/ ^ [^ 0-9] [A-z0-9 _] + ([[[[[ [A-z0-9_] +) * [@] [A-0-9 _] + ([.] [A-0-9 _] +) * [.] [Ed] {2,4} $ / ", $ Value); } // This function does not work validate_string ($ value) {return preg_match ("([^ [<
If I send an email to validate_email
the first function works well, it is used to validate me, I can get back 1
or 0
if not.
Do validate_string
with all strings, but without ? = & Lt; > / \
. If I check the function, then I return 1 in any way, why?
Valid_string ("Tonino"); // Return 1 ok validate_string ("ton \ ino \"); // Why return 1? Validate_string ("tons? Ed = 3"); // Why return 1?
^
Around ([^ should not mean characters after it ( Or
There are many errors in your code except that "ton \ ino \ "
is not a valid string and [^ ^ lt; & Gt ;? = / \] +
is not a valid regular expression, possibly some rational misconceptions.
Your regular expression [^
& lt;
, & gt;
, ?
, =
, /
and \
. So if there are at least one such letter, then preg_match
returns 1
. ton \ ino "
and tons? Asd = 3
both have at least one letter (match is ton
) in both cases.
A fix for this is either to claim the beginning and end of the string ( ^
and $
), so that only for the whole Allow Legal Characters String:
^ [^ <<
Or a positive character square To use [
:
function validate_string ($ value) {Return! Preg_match ("([[lt; & gt;? = / \\\\] +) ", $ Value);}
But it would definitely be better to use the whitelist instead of the blacklist.
Comments
Post a Comment