It’s easy to strip non-numbers from a php string. This is common when attempting to format fields like phone numbers or money amounts for entry into a database.

For instance, a website or application may allow the user to enter phone numbers in any format.

If you strip all non-numbers from the following 3 items and end up with same 10 digit phone number.

  • (800)555-0100
  • 800.555.0100
  • 800-555-0100

Use the following simple code to remove all characters from a string that are not numbers. This is perfect for PHP 5.3.

$str = preg_replace('[\D]', '', $str);

Be sure to check out the article for stripping away all but alphanumeric characters.