Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: Strip non alpha numeric or punctuation

Tags:

How can I use PHP to strip out all characters that are NOT alpha, numeric, space, or puncutation?

I've tried the following, but it strip punctuation.

preg_replace("/[^a-zA-Z0-9\s]/", "", $str); 
like image 373
Tedd Avatar asked Jun 16 '10 02:06

Tedd


People also ask

How do you remove a non-alphanumeric character from a string?

You can use the regular expression [^a-zA-Z0-9] to identify non-alphanumeric characters in a string and replace them with an empty string. Use the regular expression [^a-zA-Z0-9 _] to allow spaces and underscore character. The word characters in ASCII are [a-zA-Z0-9_] .

How do I strip non-alphanumeric characters in C#?

Using Regular Expression We can use the regular expression [^a-zA-Z0-9] to identify non-alphanumeric characters in a string. Replace the regular expression [^a-zA-Z0-9] with [^a-zA-Z0-9 _] to allow spaces and underscore character.

What is a non-alphanumeric?

Non-alphanumeric characters comprise of all the characters except alphabets and numbers. It can be punctuation characters like exclamation mark(!), at symbol(@), commas(, ), question mark(?), colon(:), dash(-) etc and special characters like dollar sign($), equal symbol(=), plus sign(+), apostrophes(').


2 Answers

preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", $str); 

Example:

php > echo preg_replace("/[^a-zA-Z0-9\s\p{P}]/", "", "⟺f✆oo☃. ba⟗r!"); foo. bar! 

\p{P} matches all Unicode punctuation characters (see Unicode character properties). If you only want to allow specific punctuation, simply add them to the negated character class. E.g:

preg_replace("/[^a-zA-Z0-9\s.?!]/", "", $str); 
like image 121
Matthew Flaschen Avatar answered Nov 03 '22 19:11

Matthew Flaschen


You're going to have to list the punctuation explicitly as there is no shorthand for that (eg \s is shorthand for white space characters).

preg_replace('/[^a-zA-Z0-9\s\-=+\|!@#$%^&*()`~\[\]{};:\'",<.>\/?]/', '', $str); 
like image 31
cletus Avatar answered Nov 03 '22 17:11

cletus