How7oHow7o
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Remove All Non-Numeric Characters from a String in PHP
Share
Notification Show More
Font ResizerAa
How7oHow7o
Font ResizerAa
  • Tools
  • Prank Screens
  • Learn
  • Blog
Search
  • Home
  • Tools
    • All Tools
    • Image Tools
    • Text & String Tools
    • Video Tools
    • Developer Tools
    • PDF Tools
    • Calculators & More
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Remove All Non-Numeric Characters from a String in PHP
Web Development

How to Remove All Non-Numeric Characters from a String in PHP

how7o
By how7o
Last updated: May 22, 2026
4 Min Read
Remove all non-numeric characters from a PHP string
SHARE

To remove all non-numeric characters from a string in PHP, use preg_replace('/\D/', '', $string) — the same regex as JavaScript’s, applied through PHP’s PCRE engine. The \D shorthand matches any character that isn’t a digit; replacing matches with the empty string leaves only the digits.

Contents
  • The one-liner
  • Explicit longhand
  • filter_var() — for signed integers
  • Keep digits and the decimal point
  • Convert to a number
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-05-17 on PHP 8.3. Originally published 2022-07-01, rewritten and updated 2026-05-17.

The one-liner

$string = 'ID NO. 94854112';
echo preg_replace('/\D/', '', $string);
// 94854112

\D matches any single non-digit character. preg_replace walks the entire string and replaces each match with the empty string — no g flag needed because PCRE replaces all matches by default.

PHP keep only digits — preg_replace \\D regex, filter_var, decimal+sign variants

Explicit longhand

echo preg_replace('/[^0-9]/', '', $string);
// 94854112

Same result, more explicit. The negated character class [^0-9] reads as “any character that is not a digit 0–9.”

filter_var() — for signed integers

$string = 'Balance: -$1,299';
echo filter_var($string, FILTER_SANITIZE_NUMBER_INT);
// -1299

Keeps digits, +, and -. Drops everything else including spaces, currency, and commas. Useful when you want a sign-aware result without writing a regex.

Keep digits and the decimal point

$price   = 'USD $1,299.95';
$numeric = preg_replace('/[^0-9.]/', '', $price);
// 1299.95

floatval($numeric);   // 1299.95

// Sign + decimal
$balance = '$-1,299.95';
$signed  = preg_replace('/[^0-9.\-]/', '', $balance);
// -1299.95

Use filter_var($price, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION) as the built-in alternative — it keeps digits, the sign, and one dot. For more control, the regex is more explicit.

Convert to a number

$digits = preg_replace('/\D/', '', 'ID NO. 94854112');

(int)   $digits;      // 94854112
intval($digits, 10);  // 94854112  — always pass the base
floatval($digits);    // 94854112.0

Frequently asked questions

Why is the slash in '/\D/' doubled in some examples?

The slashes wrap the regex pattern — they’re delimiters, not part of the pattern. '/\D/' is the standard form. Some examples use other delimiters ('#\D#', '~\D~') when the pattern itself contains slashes; you can pick any non-alphanumeric character as the delimiter.

Should I use preg_replace() or filter_var()?

filter_var($str, FILTER_SANITIZE_NUMBER_INT) keeps digits, +, and - — strict integer cleanup. preg_replace('/\D/', '', $str) drops everything that isn’t a digit (no sign, no decimal). Use filter_var when you want a signed integer; use the regex when you want pure digits.

How do I keep digits and the decimal point?

Allow the dot in the character class: preg_replace('/[^0-9.]/', '', $str). For locale-aware numbers where the decimal might be a comma (e.g. '1,99' in German), include both: '/[^0-9.,]/' and decide which to use as the separator before floatval().

Does \D match Unicode non-digits in PHP?

Only when you add the u flag: preg_replace('/\D/u', '', $str). Without it, \D works in single-byte mode. The Unicode mode matters if your input might contain non-ASCII digits like Arabic numerals (٠-٩) — without u, those are treated as non-digit characters and stripped.

Related guides

  • How to Keep Only Numbers in a String with JavaScript
  • How to Remove Unwanted Characters from a String in PHP
  • How to Get the First Character of a String in PHP

References

PHP preg_replace(): php.net/manual/en/function.preg-replace.php. PHP filter_var(): php.net/manual/en/function.filter-var.php. PCRE syntax: php.net/manual/en/regexp.reference.escape.php.

TAGGED:phpregexstrings

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Reload DataTables with a new Ajax URL How to Reload DataTables with a New Ajax URL
Next Article Completely remove MariaDB from a RHEL-family server How to Remove MariaDB Completely from RHEL/CentOS
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

You Might Also Like

WordPress posts by date range — date_query with after/before/inclusive
Web Development

How to Get Posts by Date Range in WordPress

7 Min Read
Keep the first N characters of a JavaScript string with slice
Web Development

How to Keep Only the First N Characters of a String in JavaScript

4 Min Read
Fix 409 Conflict error in Laravel (cookies, cache, WAF)
Web Development

How I Fixed the 409 Conflict Error in Laravel (Cookie / Browser / WAF Fix)

7 Min Read
Run Laravel queue workers with Supervisor
Web Development

How to Run Laravel Queue Workers in Production with Supervisor

12 Min Read
How7oHow7o

Free browser-based tools, prank screens, and step-by-step tech tutorials. No signup, nothing uploaded.

  • Private by design — your files never leave your device
  • Instant — no signup, no install, no waiting
  • Always free — no trials, no watermarks, no paywalls

Tools

  • PDF Editor
  • Image Upscaler
  • JPEG Compressor
  • Password Generator
  • QR Code Generator
  • Video Trimmer
  • See all tools →

Pranks

  • Cracked Screen Prank
  • Fake Blue Screen (BSOD)
  • Fake Disk Format
  • Fake Low Battery
  • Fake Nintendo Switch Update Screen
  • Fake PlayStation Update Screen
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?