ISSUE :
 
Profile photo
Submitted by Tim Joseph
on June 13th, 2011 at 13:47:39 EST

remove special character from string in PHP

I need to remove any special character like . \ > < etc etc from my string. I want to exclude everything except character and number. What would be the best way to achieve this in php? I tried htmlspecialchars() method but that is not exactly what I am looking for. I just want to remove everything but character and number.


Profile photo
Replied by RaKumar
on June 13th, 2011 at 14:27:31 EST
Try this out:
$textAlphanumeric = preg_replace('/[^\w\d ]/si', '', $originalText);

This regular expression replacement will replace anything beside character and number (digit).
Accepted Answer
Accepted
 Answer

Profile photo
Replied by Tim Joseph
on June 13th, 2011 at 18:04:13 EST
Thanks for great solution !