How can I make a regex pattern for use with the PHP preg_replace function that removes all characters which do not fit in a certain pattern. For example:
[a-zA-Z0-9]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can negate the character set by using
^:[^a-zA-Z0-9]The
^only negates the existing character set[...]it is in, and it only applies when it is the first character inside the set. You can read more about negated character sets hereSo, finally:
Edit:
As noted in the comments below, you can also add the
+quantifier so consecutive invalid characters will be replaced in 1 match ofpreg_replace‘s iteration: