Page 1 of 1

remove special chars

Posted: Tue Feb 06, 2007 7:03 am
by tommy1987
I have searched around for a method to remove all special characters (question marks, commas etc.) from some text, but can't seem to find one is there one which will turn it into plain text.

Sample input: 'this is a question, do you like it?'
Sample output: 'this is a question do you like it'

Have tried php.net but you guys will probaby know better.

Thanks very much, tom

Posted: Tue Feb 06, 2007 7:40 am
by dhrosti
I would probably use str_replace()

Code: Select all

$string = "this is a question, do you like it?";
$string = str_replace(',', '', $string);
$string = str_replace('?', '', $string);
It will output "this is a question do you like it"
I dont know much php but at least its a start! and theres only a handful of special characters like that i think.

Posted: Tue Feb 06, 2007 10:43 am
by feyd
You're probably looking more for a regular expression, although a str_replace() can do it (sometimes faster too).

Posted: Tue Feb 06, 2007 11:14 am
by Ollie Saunders

Code: Select all

preg_replace('~[^\w\d\s]~i', '', $str);
removes everything that isn't a word, digit or whitespace character. untested.