php - Validate string against regex -
i'd validate file paths regular expression. far i've come this:
preg_match('/[^a-za-z0-9\.\/\\\\]/', $string); so return 1 if string has other characters a-z, a-z, 0-9, dot, \ , /
how can make returns 1 if there more 2 dots in string, or if dot @ end of string?
and how can allow : if it's present 2nd character , followed \ or /. example c:\file should valid
for first 2 requirements:
preg_match('/[^a-za-z0-9\.\/\\\\]|\..*\.|\.$/', $string); the \..*\. match if there more 2 dots. \.$ match if there dot @ end. separating each of these portions (including original regex) | make regex match 1 of expressions (this called alternation).
the last requirement little tricky, since if understand correctly need return 1 if there :, unless colon second character , followed \ or /. following regex (as php string) should that:
'/:(?!(?<=^[a-za-z]:)[\/\\\\])/' or combined other regex (note have add : first character class):
preg_match('/[^a-za-z0-9\.\/\\\\:]|\..*\.|\.$|:(?!(?<=^[a-za-z]:)[\/\\\\])/', $string); here explanation last piece:
: # match ':' (?! # fail if following regex matches (negative lookahead) (?<=^[a-za-z]:) # ':' second character in string [\/\\\\] # next character '/' or '\' ) # end lookahead
Comments
Post a Comment