Josh R
Josh R

Reputation: 1295

How can I strip commas and periods from a string?

I have string containing something like this:

"Favourite bands: coldplay, guns & roses, etc.,"

How can I remove commas and periods using preg_replace?

Upvotes: 34

Views: 68591

Answers (1)

user229044
user229044

Reputation: 239240

You could use

preg_replace('/[.,]/', '', $string);

but using a regular expression for simple character substitution is overkill.

You'd be better off using strtr:

strtr($string, array('.' => '', ',' => ''));

or str_replace:

str_replace(array('.', ','), '' , $string);

Upvotes: 131

Related Questions