remove special chars

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

remove special chars

Post 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
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're probably looking more for a regular expression, although a str_replace() can do it (sometimes faster too).
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Post Reply