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

  • No Related Post

8 Responses to “Function to remove all non-numeric characters in PHP”

  1. Paulius  on October 8th, 2009

    Is it worth entire blog post?

  2. bryan  on October 12th, 2009

    Is it worth an entire comment?

  3. wes  on November 12th, 2009

    The answer is yes.

  4. donwilson  on November 13th, 2009

    Is it worth an entire reply?

  5. James  on November 13th, 2009

    Actually, it’s worth it. Thank you for this.

  6. bryan  on November 14th, 2009

    You’re welcome.

  7. wefwef  on November 14th, 2009

    Line 2 is missing a “;”.

    This is more readable I think:

    preg_replace(’/[^0-9]/Uis’, ”, $string);

    Cheers

  8. bear  on December 7th, 2009

    THK 4 this simple but useful.


Leave a Reply