What is the regular expression for this condition?
i want the regular expression to sanitize my data which should meet the following condition
a) a-z and A-Z allowed
b) 0-9 allowed
c) Special Symbols like Comma (,) dot (.) question Mark (? allowed)
d) Single Space is allowed
i tried and came up with this
preg_replace('%[^a-zA-Z0-9,.?\s]%', '', $string);
i am not so familiar with RegExp, although the above code works, i would like to know
a) if i am using the correct RegExp syntax?
b) if i don't use
modulus开发者_JS百科(%)
at the start and end of the syntax it won't work, and i have no idea what is the purpose of modulus here?
If you are requirement is to allow any single whitespace ( which includes space, newline, horizontal tab, vertical tab, form feed etc) then your regex is correct. But if you want to allow only spaces then change the \s
to .
The call the
preg_replace
deletes all non-allowed characters from the input.
The %
in the call to preg_replace
are used as regex delimiters. The preg_
family of functions expect the regex to be within a pair of delimiters. My answer here talks more on what can be used as delimiters.
a) If it works, it's correct.
b) "When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character" - from PHP Documentation
\s is a whitespace character. Which includes tabs.
Also, if you can allow underscores, \w might make it a bit simpler
\w = [a-zA-Z0-9_] // \w is a "word" character (including underscores)
You need to escape dot, comma, and question mark
[^a-zA-Z0-9\,\.\?\s]
精彩评论