I want to remove any type of special characters in a string like this:
This is, ,,, *&% a ::; demo + String. +
Need to**@!/// format:::::
!!! this.`
Output Required:
This is a demo String Need to format this
How to do this using REGEX?
Check for any repeated instance of a non-number, non-letter character and repeat with a space:
# string(41) "This is a demo String Need to format this"
$str = trim( preg_replace( "/[^0-9a-z]+/i", " ", $str ) );
Demo: http://codepad.org/hXu6skTc
/ # Denotes start of pattern [ # Denotes start of character class ^ # Not, or negative 0-9 # Numbers 0 through 9 (Or, "Not a number" because of ^ a-z # Letters a through z (Or, "Not a letter or number" because of ^0-9 ] # Denotes end of character class + # Matches 1 or more instances of the character class match / # Denotes end of pattern i # Case-insensitive, a-z also means A-Z
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With