I would like to know how to strip all non-alphanumeric characters from a string except for underscores and dashes in PHP.
Share
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.
Use
preg_replace:The first argument to
preg_replaceis a regular expression. This one contains:/– starting delimiter — start the regex[– start character class — define characters that can be matched^– negative — make the character class match only characters that don’t match the selection that follows\w– word character — so don’t match word characters. These areA-Za-z0-9and_(underscore)-– hyphen — don’t match hypens either]– close the character class/– ending delimiter — close the regexNote that this only matches hyphens (i.e. -). It does not match genuine dash characters (– or —).