I have string containing something like this:
"Favourite bands: coldplay, guns & roses, etc.,"
How can I remove commas and periods using preg_replace
?
To remove all commas from a string, call the replace() method, passing it a regular expression to match all commas as the first parameter and an empty string as the second parameter. The replace method will return a new string with all of the commas removed.
Use Python to Remove Punctuation from a String with Translate. One of the easiest ways to remove punctuation from a string in Python is to use the str. translate() method. The translate method typically takes a translation table, which we'll do using the .
We can use the str. replace() method to remove commas from a given string as follows. Output: Original String: This, is, a, string, that, has, commas, in, it.
You can remove comma from string in python by using string's replace() method.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With