- How to remove non-alphanumeric characters from a string
- To remove non-alphanumeric characters from a string,first you need to basically defined it as a regex.
- For unicode characters, we have to use the below example:
- We can also use the regular expression.
Here is an example:
we need to note of the following
- The i stands for case insensitive.
- ^ means, does not start with.
- \d matches any digit.
- a-z matches all characters between a and z. Because of the i parameter you don’t have to specify a-z and A-Z.
- After \d there is a space, so spaces are allowed in this regex
- here’s a really simple regex for remove non-alpha numeric characters:
and used as you need it (with a forward / slash delimiter).
- Test it here with this great tool that explains what the regex is doing:
- A PHP function that strips a string of all characters other than alphanumeric characters:
- We can also use the below code to Remove All Non-Numeric Characters
- This code will delete any non-alphanumeric characters specified between the brackets, and then output/return the clean version.