Web 2.0 University

How to Remove Numbers from a String in PHP

Last edited on
🤓 Secure Your Online Presence with Namecheap Domains - Click Here!

* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.

When working with strings in PHP, you might encounter situations where you need to get the numbers in a string or remove them.

This can be useful in data cleansing, formatting text, or handling user input.

In this guide, we will explore various methods to remove numbers from a string in PHP, along with their advantages and disadvantages.


Using preg_replace() to Remove Numbers

One of the most effective ways to remove numbers from a string is by using preg_replace() with a regular expression.

<?php
$string = "Order123 has been processed on 2024-02-17";
$clean_string = preg_replace('/\d+/', '', $string);
echo $clean_string; // Output: "Order has been processed on -"
?>

Pros:

  • Powerful and flexible.
  • Can remove all numbers, including decimals.
  • Works with both single and multi-digit numbers.

Cons:

  • Requires knowledge of regular expressions.
  • May remove numeric characters unintentionally if not carefully designed.

Using str_replace() to Remove Specific Numbers

If you need to remove only certain numbers, str_replace() can be a simple solution.

<?php
$string = "Invoice #2025, Amount: $1000";
$clean_string = str_replace(['2025', '1000'], '', $string);
echo $clean_string; // Output: "Invoice #, Amount: $"
?>

Pros:

  • Easy to use for known numbers.
  • No need for regular expressions.

Cons:

  • Not scalable for removing all numbers.
  • Requires listing each number to be removed.

Removing Numbers Using filter_var()

filter_var() can be useful for removing numbers while keeping non-numeric characters.

<?php
$string = "Product Code: A123B456";
$clean_string = filter_var($string, FILTER_SANITIZE_STRING);
echo $clean_string; // Output: "Product Code: AB"
?>

Pros:

  • Simple and built into PHP.
  • Removes only numeric values.

Cons:

  • May not work effectively for all cases.
  • Some PHP versions may not support FILTER_SANITIZE_STRING as expected.

Using strtr() for Number Removal

Another method is strtr(), which replaces specific characters in a string.

<?php
$string = "Room 405, Floor 12";
$clean_string = strtr($string, ['0' => '', '1' => '', '2' => '', '4' => '', '5' => '']);
echo $clean_string; // Output: "Room , Floor "
?>

Pros:

  • Allows fine-grained control over removed characters.
  • Simple and efficient.

Cons:

  • Not practical for removing all numbers dynamically.
  • Requires listing each digit manually.

Removing Numbers with preg_replace_callback()

If you need more control over number removal, you can use preg_replace_callback().

<?php
$string = "Employee ID: 98765, Batch 42";
$clean_string = preg_replace_callback('/\d+/', function($matches) {
    return '';
}, $string);
echo $clean_string; // Output: "Employee ID: , Batch "
?>

Pros:

  • Provides advanced customization options.
  • Useful for handling complex string structures.

Cons:

  • More complex than simpler methods.
  • Requires callback function logic.

Summary of Methods

MethodBest ForProsCons
preg_replace()Removing all numbersFast and flexibleRequires regex knowledge
str_replace()Removing specific numbersEasy to useNot scalable for all numbers
filter_var()Removing numeric valuesSimpleMay not work in all cases
strtr()Removing selected digitsFine-grained controlManual setup required
preg_replace_callback()Advanced number removalHighly customizableMore complex setup

Conclusion

Removing numbers from a string in PHP can be done in multiple ways, each suited for different use cases. Whether you need a quick solution like str_replace() or a more powerful approach like preg_replace(), choosing the right method ensures efficient data processing.


Additional Resources

Get up to 65% Off Hosting with FREE SSL & FREE Domains!