For example suppose I have
$blah = "C$#@#.a534&";
I wish to filter the string so that only letters, numbers and "." remain yielding "C.a534"
How do I do this?
If you know what characters should be allowed, you can use a negated character group (in a regular expression) to remove everything else:
$blah = preg_replace('/[^a-z0-9\.]/i', '', $blah);
Note that i am using the i
modifier for the regular expression. It matches case-insensitive, so that we do not need to specify a-z
and A-Z
.
been answered lots of times but:
function cleanit($input){
return preg_replace('/[^a-zA-Z0-9.]/s', '', $input);
}
$blah = cleanit("C$#@#.a534&");
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