Function to remove all non-numeric characters in PHP
Sometimes it’s helpful to ONLY get numeric characters.
Phone numbers, age — whatever your little PHP heart desires.
function remove_non_numeric($string) {
return preg_replace('/\D/', '', $string)
}
WRAAAAAAA,
Bryan
Related reading
10 Responses to “Function to remove all non-numeric characters in PHP”
Leave a Reply

Paulius on October 8th, 2009
Is it worth entire blog post?
bryan on October 12th, 2009
Is it worth an entire comment?
wes on November 12th, 2009
The answer is yes.
donwilson on November 13th, 2009
Is it worth an entire reply?
James on November 13th, 2009
Actually, it’s worth it. Thank you for this.
bryan on November 14th, 2009
You’re welcome.
wefwef on November 14th, 2009
Line 2 is missing a “;”.
This is more readable I think:
preg_replace(’/[^0-9]/Uis’, ”, $string);
Cheers
bear on December 7th, 2009
THK 4 this simple but useful.
Fishing on May 13th, 2010
Yes it absolutely was. I’m sending out text message notifications for new fishing reports and have to strip non-numeric chars. Thanks!
Mr Incredible on May 27th, 2010
It was a rather short post but handy. Imparting any knowledge shouldn’t be criticized.
Paulius, you wouldn’t be here if you didn’t need to know about it right?