Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
开发者_StackOverflow中文版 Improve this questionhaving a bit of trouble with replacing things that are NOT 0-9A-Za-z[:space] because I cannot find a NOT metachar for preg_replace. Does anytone know if one exists and if not what is the best way to strip anything that is NOT alpha numeric or a space?
Use negated character classes:
/[^A-Za-z0-9 ]/
You could also use the \w
escape sequence, which is equivalent to [a-zA-Z0-9_]
(note the underscore). So your regex would look like
/[^\w ]/
Reference:
http://www.regular-expressions.info/charclass.html
More useful:
/[^A-Za-z0-9 ]+$/
精彩评论