I’ve got text from which I want to remove all characters that ARE NOT the following.
desired_characters =
0123456789!&',-./abcdefghijklmnopqrstuvwxyz\n
The last is a \n (newline) that I do want to keep.
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.
To match all characters except the listed ones, use an inverted character set
[^…]:Here
preg_quoteis used to escape certain special characters so that they are interpreted as literal characters.You could also use character ranges to express the listed characters:
In this case it doesn’t matter if the literal
-in,-.is escaped or not. Because,-.is interpreted as character range from,(0x2C) to.(0x2E) that already contains the-(0x2D) in between.Then you can remove those characters that are matched with
preg_replace: