+ 2
help php
How to remove in string all characters except letters, numbers, and spaces?
2 Answers
+ 5
use regex :
//String containing special characters.
$str = "this is- a' (test*&()";
//Remove any character that isn't A-Z, a-z, 0-9 or a whitespace.
$str = preg_replace("/[^A-Za-z0-9 ]/", '', $str);
+ 2
If the final goal is to prevent xss attack, you can use htmlspecialchars methode to convert special characters to HTML entities.