Ryan Arief Ryan Arief - 1 year ago 47
PHP Question

PHP remove numbers only

Lets say I have a word like this

"I shoot someone using ak47 and m4s 32 times"

What is the best way to remove pure number number so that I only get
"I shoot someone using ak47 and m4s times"

I will be really glad if someone can teach me how to do that.


You could do that with a regex checking for word boundaries and digits:


Here you are checking for a word-boundary, followed by any number of digits followed by another word boundary. The forward slashes are the delimiters.

Then you can use for example preg_replace to replaced the matched numbers with an empty string:

$result = preg_replace('/\b\d+\b/', '', $your_string);

Note that you will end up with 2 spaces between m4s and times but you will not see that in html output. If necessary, you can search for these as well.