How to remove all numbers from string?

How to remove all numbers from string?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your Current Approach Is Scalability Nightmare and How to Fix It

The code snippet you are looking at works, but it is a textbook example of what we call "script bloat." You are iterating through every single digit character from 0 to 9, executing a separate preg_replace call for each one. While this eventually removes the numbers, it introduces unnecessary computational overhead and significantly reduces code readability. In a production environment, you might find yourself needing to handle ranges of digits or specific patterns; writing ten lines of repetitive code makes that impossible without refactoring into a loop, which defeats the purpose of keeping it concise.

The elegant solution lies in leveraging the power of regular expressions (regex) to define a character class rather than handling each digit individually. In PHP, you can remove all numeric characters from a string in a single line using the following pattern:

$cleaned = preg_replace('/\d/', '', $words);

This pattern is significantly more robust. The \d metacharacter matches any decimal digit (equivalent to [0-9]). By placing it inside the preg_replace function without a specific replacement string, you effectively tell PHP to replace every match with an empty string. This approach scales effortlessly; if you later decide you need to handle Unicode digits or different locales, the logic remains contained within that single line.

Handling Diacritics and Unicode Correctly

You mentioned encountering issues where other solutions remove diacritics like á, ñ, or ž. This is a common pitfall when using standard regex functions without specifying the correct character encoding context. The issue usually arises because \d in standard PCRE (Perl Compatible Regular Expressions) only matches ASCII digits (0-9). It does not inherently target letters with accents, but if your source text contains non-breaking spaces or other invisible Unicode characters often associated with these diacritics, a poorly constructed regex might accidentally strip them.

To ensure you are strictly targeting numbers and preserving the integrity of accented characters, you must be mindful of your PHP intl extensions and locale settings. However, for standard ASCII text, the \d pattern is safe. If you find that your specific environment is stripping accents, it is likely because the regex engine is interpreting the input string's encoding differently than expected, or a different character class like \p{Nd} (general decimal number) was used, which might have broader implications depending on the PHP version and installed extensions.

The preg_match_all Alternative for Extraction

Sometimes, instead of removing characters, you want to isolate them. If your goal shifts from removal to extraction, preg_match_all('/\d/', $words, $matches) is the standard tool. This function returns an array containing all found matches. You can then reconstruct the string without those numbers using PHP's built-in string manipulation functions like str_replace or by iterating over the original string and building a new one based on character codes.

For example, if you need to remove numbers but keep the positions for alignment purposes, a loop checking ctype_digit() is often faster than invoking the regex engine repeatedly. The performance difference between a single preg_replace with \d and a manual loop becomes negligible unless you are processing millions of strings per second. However, readability always favors the regex approach.

Where to Look for Advanced Patterns

If your requirements evolve beyond simple ASCII digit removal—perhaps you need to handle full-width numbers used in Asian text or specific numeric formats defined by Unicode—the standard \d might not be sufficient. In those cases, exploring resources that specialize in complex string manipulation patterns can help. For instance, looking into https://laravelcompany.com often provides insights into how modern frameworks handle internationalization (i18n) and string cleaning, which is crucial when dealing with global user inputs where á and a might need distinct handling depending on the business logic.

The key takeaway is to avoid the brute-force method of chaining ten separate replacements. Always prefer a character class that encapsulates your intent in one logical step. This keeps your codebase clean, performant, and easier for other developers (or yourself) to maintain.

Our network