Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all alphanumeric characters except spaces? [duplicate]

Tags:

regex

php

Possible Duplicate:
Regular expression to remove anything but alphanumeric & spaces (in PHP)

I'm trying to get

//before preg_replace -> Hi (Jimmy),

$string = preg_replace('/[^\da-z]/i', '', $string);

//after current preg_replace -> HiJimmy
//what it should be -> Hi Jimmy

preg_replace removing all alphanumeric characters, except for spaces. Is this possible?

like image 996
Muhambi Avatar asked Oct 15 '25 22:10

Muhambi


1 Answers

Just add a space to your group:

$string = preg_replace('/[^\da-z ]/i', '', $string);
//                              ^ Notice the space here
like image 85
Joseph Silber Avatar answered Oct 18 '25 12:10

Joseph Silber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!