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
8 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.